explain ground-thruth .mat file of an image for CNN
good evening,
i'm new to coding CNN i'v got ShanghaiTech crowd counting dataset that has (beside the images) .mat files for what i believe the ground truth for (counting) for images.
i try to print the content of one .mat file in python, here is what i get:
{'image_info': array([[array([[(array([[ 855.32345978, 590.49587357],
[ 965.5908524 , 472.79472415],
[ 937.09478464, 400.93507502],
...,
[ 42.5852337 , 359.87860699],
[1017.48233659, 8.99748811],
[1017.48233659, 23.31916643]]), array([[920]], dtype=uint16))]],
dtype=[('location', 'O'), ('number', 'O')])]], dtype=object), '__version__': '1.0', '__header__': 'MATLAB 5.0 MAT-file, Platform: PCWIN64, Created on: Fri Nov 18 20:06:05 2016', '__globals__': []}
each .mat file corresponds to one image, i know at some point in CNN we need to calculate the error between the network result and the ground truth we have, but i don't seem to understand the structure and the content of these .mat files.
can someone explain whats in these files and how or for what that content is used in crowd estimation.
See also questions close to this topic
-
How to train a CNN on a UCI dataset in MATLAB?
I am fairly new with CNNs at the moment. I am trying to train a CNN on UCI datasets which are basically flat files csv to be more precise. I could easily do ML based tasks using other classifiers on Matlab but I am fairly stuck with CNNS. From what I can gather is that a 1d CNN will be utilized to classify the file but anyone has any idea of one can achieve that? Right now that code I am using is as follows:
X = Xtrain; y = Ytrain; height = size(X, 2); width = 1; channels = 1; sampleSize = size(X,1); noOfClasses = length(unique(y)); CNN_Xtrain = reshape(X,[height, width, channels, sampleSize]); CNN_Ytrain = categorical(y); layers = [imageInputLayer([height, width, channels]) convolution2dLayer([1], 100) batchNormalizationLayer reluLayer fullyConnectedLayer(noOfClasses) softmaxLayer classificationLayer]; options = trainingOptions('adam',... 'MaxEpochs', 1000 ... ); layers(2).Weights = randn([1 1 1 100]) * 0.0001; layers(2).Bias = randn([1 1 100])*0.00001 + 1; classifier = trainNetwork(CNN_Xtrain, CNN_YTrain, layers, options);
But with this code my accuracy is really bad. Please help.
-
How to calculate volume under a plane in matlab?
I want to calculate the volume under a plane limited by three point, three point is P1,P2, P3
P1 = [575,0,400]; P2 = [287.5,0,662]; P3 = [575,3500,154]; normal = cross(P1-P2, P1-P3) syms x y z P = [x, y, z] ep1=dot(normal, P-P1) % get the equation Z = solve(ep1,z) [Xq,Yq] = meshgrid(-3500:0.5:3500); f=@(x,y)(interp2(Z,Xq,Yq)) % I want to calculate volume under the plane ranged by Xmin=2.875, Xmax=575,Ymin=0,Ymax=3500 volume = quad2d(f,(287.5),575,0,3500) %volume = integral2(f,287.5,575,0,3500)
But my code give Sample values must be a single or double array. How to fix it thanks a lot.
-
MATLAB: quadprog index issue?
I am trying to run several files of code for an assignment. I am trying to solve an optimization problem using the "quadprog" function from the "optim" package.
quadprog is supposed to solve the optimization problem in a certain format and takes inputs H,f, A,b, Aeq, Beq, lb, ub.
The issue I am having involves my f which is a column vector of constants. To clarify, f looks like c*[1,1,1,1,1,1] where c is a constant. Quadprog seems to run my code just fine for certain values of c, but gives me the error:
error: index (_,49): but object has size 2x2
error: called from quadprog at line 351 column 32
for other values of c. So, for example, 1/3 works, but 1/2 doesn't. Does anyone have any experience with this?
Sorry for not providing a working example. My code runs over several files and I seem to only be having problems with a specific value set that is very big. Thanks!
-
VGG16 Tensorflow implementation does not learn on cifar-10
This VGGNet was implemented using Tensorflow framework, from scratch, where all of the layers are defined in the code. The main problem I am facing here is that the training accuracy, not to mention validation accuracy, goes up even though I wait it out for a decent amount of time. There are few problems that I suspect is causing this problem right now. First, I think the network is too deep and wide for cifar-10 dataset. Second, extracting data batch out of the whole dataset is not exhaustive, i.e. Batch selection is used over and over again over the whole dataset without eliminating those examples that were selected in the ongoing epoch.
However, still I could not get this code to work after many hours and days of experiments.
I wish I could extract the problematic code section to ask a question, but since I cannot pinpoint the exact section here, let me upload my whole code.
import os import sys import tensorflow as tf import numpy as np import scipy as sci import math import matplotlib.pyplot as plt import time import random import imageio import pickle import cv2 import json from pycocotools.coco import COCO class SVGG: def __init__(self, num_output_classes): self.input_layer_size = 0 self.num_output_classes = num_output_classes # Data self.X = [] self.Y = [] self.working_x = [] self.working_y = [] self.testX = [] self.testY = [] # hard coded for now. Have to change. self.input_data_size = 32 # 32 X 32 self.input_data_size_flat = 3072 # 32 X 32 X 3 == 3072 self.num_of_channels = 3 # 3 for colour image self.input_data_size = 32 # 32 X 32 self.input_data_size_flat = self.input_data_size * self.input_data_size # 32 X 32 X 3 == 3072 self.num_of_channels = 3 # 3 for colour image self.convolution_layers = [] self.convolution_weights = [] self.fully_connected_layers = [] self.fully_connected_weights = [] def feed_examples(self, input_X, input_Y): """ Feed examples to be learned :param input_X: Training dataset X :param input_Y: Traning dataset label :return: """ # Take first input and calculate its size # hard code size self.X = input_X self.Y = input_Y self.input_data_size_flat = len(self.X[0]) * len(self.X[0][0]) * len(self.X[0][0][0]) def feed_test_data(self, test_X, test_Y): self.testX = test_X self.testY = test_Y def run(self): x = tf.placeholder(tf.float32, [None, self.input_data_size_flat], name='x') x_data = tf.reshape(x, [-1, self.input_data_size, self.input_data_size, 3]) y_true = tf.placeholder(tf.float32, [None, self.num_output_classes], name='y_true') y_true_cls = tf.argmax(y_true, axis=1) """ VGG layers """ # Create layers ######################################## Input Layer ######################################## input_layer, input_weight = self.create_convolution_layer(x_data, num_input_channels=3, filter_size=3, num_filters=64, use_pooling=True) # False ######################################## Convolutional Layer ######################################## ############### Conv Layer 1 ################# conv_1_1, w_1_1 = self.create_convolution_layer(input=input_layer, num_input_channels=64, filter_size=3, num_filters=64, use_pooling=False) conv_1_2, w_1_2 = self.create_convolution_layer(input=conv_1_1, num_input_channels=64, filter_size=3, num_filters=128, use_pooling=True) ############### Conv Layer 2 ################# conv_2_1, w_2_1 = self.create_convolution_layer(input=conv_1_2, num_input_channels=128, filter_size=3, num_filters=128, use_pooling=False) conv_2_2, w_2_2 = self.create_convolution_layer(input=conv_2_1, num_input_channels=128, filter_size=3, num_filters=256, use_pooling=True) ############### Conv Layer 3 ################# conv_3_1, w_3_1 = self.create_convolution_layer(input=conv_2_2, num_input_channels=256, filter_size=3, num_filters=256, use_pooling=False) conv_3_2, w_3_2 = self.create_convolution_layer(input=conv_3_1, num_input_channels=256, filter_size=3, num_filters=256, use_pooling=False) conv_3_3, w_3_3 = self.create_convolution_layer(input=conv_3_2, num_input_channels=256, filter_size=3, num_filters=512, use_pooling=True) ############### Conv Layer 4 ################# conv_4_1, w_4_1 = self.create_convolution_layer(input=conv_3_3, num_input_channels=512, filter_size=3, num_filters=512, use_pooling=False) conv_4_2, w_4_2 = self.create_convolution_layer(input=conv_4_1, num_input_channels=512, filter_size=3, num_filters=512, use_pooling=False) conv_4_3, w_4_3 = self.create_convolution_layer(input=conv_4_2, num_input_channels=512, filter_size=3, num_filters=512, use_pooling=True) ############### Conv Layer 5 ################# conv_5_1, w_5_1 = self.create_convolution_layer(input=conv_4_3, num_input_channels=512, filter_size=3, num_filters=512, use_pooling=False) conv_5_2, w_5_2 = self.create_convolution_layer(input=conv_5_1, num_input_channels=512, filter_size=3, num_filters=512, use_pooling=False) conv_5_3, w_5_3 = self.create_convolution_layer(input=conv_5_2, num_input_channels=512, filter_size=3, num_filters=512, use_pooling=True) layer_flat, num_features = self.flatten_layer(conv_5_3) ######################################## Fully Connected Layer ######################################## fc_1 = self.create_fully_connected_layer(input=layer_flat, num_inputs=num_features, num_outputs=4096) fc_2 = self.create_fully_connected_layer(input=fc_1, num_inputs=4096, num_outputs=4096) fc_3 = self.create_fully_connected_layer(input=fc_2, num_inputs=4096, num_outputs=self.num_output_classes, use_dropout=False) # Normalize prediction y_prediction = tf.nn.softmax(fc_3) # The class-number is the index of the largest element y_prediction_class = tf.argmax(y_prediction, axis=1) # Cost-Fuction to be optimized cross_entropy = tf.nn.softmax_cross_entropy_with_logits_v2(logits=fc_3, labels=y_true) # => Now we have a measure of how well the model performs on each image individually. But in order to use the # Cross entropy to guide the optimization of the model's variable swe need a single value, so we simply take the # Average of the cross-entropy for all the image classifications cost = tf.reduce_mean(cross_entropy) # Optimizer optimizer_adam = tf.train.AdamOptimizer(learning_rate=0.002).minimize(cost) # Performance measure correct_prediction = tf.equal(y_prediction_class, y_true_cls) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) total_iterations = 0 num_iterations = 100000 start_time = time.time() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) for i in range(num_iterations): x_batch, y_true_batch, _ = self.get_batch(X=self.X, Y=self.Y, low=0, high=40000, batch_size=128) feed_dict_train = {x: x_batch, y_true: y_true_batch} sess.run(optimizer_adam, feed_dict_train) if i % 100 == 99: # Calculate the accuracy on the training-set. x_batch, y_true_batch, _ = self.get_batch(X=self.X, Y=self.Y, low=40000, high=50000, batch_size=1000) feed_dict_validate = {x: x_batch, y_true: y_true_batch} acc = sess.run(accuracy, feed_dict=feed_dict_validate) # Message for printing. msg = "Optimization Iteration: {0:>6}, Training Accuracy: {1:>6.1%}" # print(sess.run(y_prediction, feed_dict=feed_dict_train)) # print(sess.run(y_prediction_class, feed_dict=feed_dict_train)) print(msg.format(i + 1, acc)) if i % 10000 == 9999: oSaver = tf.train.Saver() oSess = sess path = "./model/_" + "iteration_" + str(i) + ".ckpt" oSaver.save(oSess, path) if i == num_iterations - 1: x_batch, y_true_batch, _ = self.get_batch(X=self.testX, Y=self.testY, low=0, high=10000, batch_size=10000) feed_dict_test = {x: x_batch, y_true: y_true_batch} test_accuracy = sess.run(accuracy, feed_dict=feed_dict_test) msg = "Test Accuracy: {0:>6.1%}" print(msg.format(test_accuracy)) def get_batch(self, X, Y, low=0, high=50000, batch_size=128): x_batch = [] y_batch = np.ndarray(shape=(batch_size, self.num_output_classes)) index = np.random.randint(low=low, high=high, size=batch_size) counter = 0 for idx in index: x_batch.append(X[idx].flatten()) y_batch[counter] = one_hot_encoded(Y[idx], self.num_output_classes) y_batch_cls = Y[idx] counter += 1 return x_batch, y_batch, y_batch_cls def generate_new_weights(self, shape): w = tf.Variable(tf.truncated_normal(shape, stddev=0.05)) return w def generate_new_biases(self, shape): b = tf.Variable(tf.constant(0.05, shape=[shape])) return b def create_convolution_layer(self, input, num_input_channels, filter_size, num_filters, use_pooling): """ :param input: The previous layer :param num_input_channels: Number of channels in previous layer :param filter_size: W and H of each filter :param num_filters: Number of filters :return: """ shape = [filter_size, filter_size, num_input_channels, num_filters] weights = self.generate_new_weights(shape) biases = self.generate_new_biases(num_filters) layer = tf.nn.conv2d(input=input, filter=weights, strides=[1, 1, 1, 1], padding='SAME') layer += biases # Max Pooling if use_pooling: layer = tf.nn.max_pool(layer, [1, 2, 2, 1], [1, 2, 2, 1], padding='SAME') # ReLu. Using elu for better performance layer = tf.nn.elu(layer) return layer, weights def create_fully_connected_layer(self, input, num_inputs, num_outputs, use_dropout=True): weights = self.generate_new_weights(shape=[num_inputs, num_outputs]) biases = self.generate_new_biases(shape=num_outputs) layer = tf.matmul(input, weights) + biases layer = tf.nn.elu(layer) if use_dropout: keep_prob = tf.placeholder(tf.float32) keep_prob = 0.5 layer = tf.nn.dropout(layer, keep_prob) return layer def flatten_layer(self, layer): """ Flattens dimension that is output by a convolution layer. Flattening is need to feed into a fully-connected-layer. :param layer: :return: """ # shape [num_images, img_height, img_width, num_channels] layer_shape = layer.get_shape() # Number of features h x w x channels num_features = layer_shape[1: 4].num_elements() # Reshape layer_flat = tf.reshape(layer, [-1, num_features]) # Shape is now [num_images, img_height * img_width * num_channels] return layer_flat, num_features def unpickle(file): with open(file, 'rb') as file: dict = pickle.load(file, encoding='bytes') return dict def convert_to_individual_image(flat): img_R = flat[0:1024].reshape((32, 32)) img_G = flat[1024:2048].reshape((32, 32)) img_B = flat[2048:3072].reshape((32, 32)) #B G R mean = [125.3, 123.0, 113.9] img = np.dstack((img_R - mean[0], img_G - mean[1], img_B - mean[2])) img = np.array(img) # img = cv2.resize(img, (224, 224), img) return img def read_coco_data(img_path, annotation_path): coco = COCO(annotation_path) ids = list(coco.imgs.keys()) ann_keys = list(coco.anns.keys()) print(coco.imgs[ids[0]]) print(coco.anns[ann_keys[0]]) def one_hot_encoded(class_numbers, num_classes=None): if num_classes is None: num_classes = np.max(class_numbers) + 1 return np.eye(num_classes, dtype=float)[class_numbers] if __name__ == '__main__': data = [] labels = [] val_data = [] val_label = [] # cifar-10 counter = 0 for i in range(1, 6): unpacked = unpickle("./cifar10/data_batch_" + str(i)) tmp_data = unpacked[b'data'] tmp_label = unpacked[b'labels'] inner_counter = 0 for flat in tmp_data: converted = convert_to_individual_image(flat) data.append(converted) labels.append(tmp_label[inner_counter]) counter += 1 inner_counter += 1 cv2.imwrite("./img/" + str(counter) + ".jpg", converted) # Test data unpacked = unpickle("./cifar10/test_batch") test_data = [] test_data_flat = unpacked[b'data'] test_label = unpacked[b'labels'] for flat in test_data_flat: test_data.append(convert_to_individual_image(flat)) svgg = SVGG(10) svgg.feed_examples(input_X=data, input_Y=labels) svgg.feed_test_data(test_X=test_data, test_Y=test_label) svgg.run()
-
Scalar Output Tensorflow JS
Using a sequential model, how do you take an array of 2d dimensional inputs (a three dimensional input) and have the model perform a prediction on each 2d input, to produce a scalar? Input shape (boards): [ 153, 8, 8 ]. Output shape (results): [153].
Model:
const model = tf.sequential(); model.add(tf.layers.dense({units: 8, inputShape: [8]})); model.add(tf.layers.dense({units:1, activation: 'sigmoid'})); // Prepare the model for training: Specify the loss and the optimizer. model.compile({loss: 'meanSquaredError', optimizer: 'sgd'}); const xs = tf.tensor(boards); const ys = tf.tensor(results); model.fit(xs, ys, {batchSize: 8, epoch: 10000}).then(() => { }); model.predict(tf.tensor(brokenFen)).print(); console.log(JSON.stringify(model.outputs[0].shape));
Output:
Tensor [[1], [1], [1], [1], [1], [1], [1], [1]] [null,1]
Desired Output:
Tensor [1]
If you have any further questions, lmk.
-
When should you update weights in neural network using backpropagation?
Let's say I have a 3 layer fully-connected neural network. I am implementing backpropagation algorithm. My question is, should I first calculate deltas and then after backpropagation is done, update the weights, or should I do it as I backpropagate through layers? I have seen both ways in internet tutorials.
I'm not sure because if I update weights during backpropagation I use newly updated weights (hidden to outputs weights) to calculate hidden layer deltas and I'm not sure is this is desired.
Sorry if I used incorrect terminology, I am new to this and trying to learn.
-
Graph Disconnected when trying to build CNN model with Keras Functional API
I am trying to build a CNN model using Keras Functional API but whenever I try to execute this line of code:
model = CNN(settings, np.expand_dims(x_train, axis = 3)).build_network()
I keep running into the following issue:ValueError: Graph disconnected: cannot obtain value for tensor Tensor("input_11:0", shape=(?, 28, 28, 1), dtype=float32) at layer "input_11". The following previous layers were accessed without issue: []
Here is my code:
class CNN: def __init__(self, settings, data): self.flag = False self.settings = settings if self.check_network_settings() == False: self.flag = True return self.data = data if K.image_data_format() == "channels_first": self.data = self.data.reshape(data.shape[0], data.shape[3], data.shape[2], data.shape[1]) self.build_network() def show_model_chart(self): if not os.path.isfile('model.png'): plot_model(self.model, to_file = 'model.png') model_pic = cv2.imread('model.png') plt.imshow(model_pic) plt.show() def build_network(self): print('Bulding Convolutional Neural Network ...') inputs = Input(shape = (self.data.shape[1], self.data.shape[2], self.data.shape[3])) final_output = None for layer_idx in range(self.settings['conv']['layers']): inputs = Conv2D( filters = self.settings['conv']['filters'][layer_idx], kernel_size = self.settings['conv']['kernel_size'][layer_idx], strides = self.settings['conv']['strides'][layer_idx], padding = self.settings['conv']['padding'] )(inputs) if self.settings['pooling']['apply'] == True: inputs = MaxPooling2D( pool_size = self.settings['pooling']['pool_size'][layer_idx], strides = self.settings['pooling']['strides'][layer_idx], padding = self.settings['pooling']['padding'] )(inputs) inputs = Activation( activation = self.settings['detector_stage'][layer_idx] )(inputs) inputs = Flatten()(inputs) for dense_layer_idx in range(self.settings['dense']['layers']): if self.settings['dense']['activations'][dense_layer_idx] != 'softmax': inputs = Dense( units = self.settings['dense']['output_units'][dense_layer_idx], activation = self.settings['dense']['activations'][dense_layer_idx] )(inputs) else: final_output = Dense( units = self.settings['dense']['output_units'][dense_layer_idx], activation = self.settings['dense']['activations'][dense_layer_idx] )(inputs) self.model = Model(inputs = inputs, outputs = final_output) def check_network_settings(self): for key in self.settings: if key == 'conv': if set(self.settings['conv'].keys()) != {'layers', 'filters', 'kernel_size', 'strides', 'padding'}: print('[INCORRECT SETTINGS]: Convolutional layers ...') return False elif key == 'pooling': if set(self.settings['pooling'].keys()) != {'apply', 'pool_size', 'strides', 'padding'}: print('[INCORRECT SETTINGS]: Pooling layers ...') return False if len(self.settings['pooling']['apply']) != self.settings['conv']['layers']: print('Please specify wether or not to apply pooling for each convolutional layer') return False elif key == 'detector_stage': if self.settings['conv']['layers'] != len(self.settings['detector_stage']): print('Number of activation functions you have specified does not match the number of convolutional layers inside the network ...') return False elif key == 'dense': if set(self.settings['dense'].keys()) != {'layers', 'output_units', 'activations'}: print('[INCORRECT SETTINGS]: Dense layers ...') return False if 'softmax' != self.settings['dense']['activations'][len(self.settings['dense']['activations'])-1]: print('Your network must contain Softmax activation function at the last Dense layer in order to produce class probabilities ...') return False print('Network settings have been correctly specified ...') return True
And here are the settings I provided as an argument to the class constructor:
settings = { 'conv': { 'layers': 3, 'filters': [32, 64, 128], 'kernel_size':[(3,3), (5,5), (5,5)], 'strides': [1, 1, 1], 'padding': 'same', }, 'pooling': { 'apply': [True, True, True], 'pool_size': [(2,2), (3,3), (3,3)], 'strides': [1, 1, 1], 'padding': 'same' }, 'detector_stage': ['relu', 'relu', 'relu'], 'dense': { 'layers': 2, 'output_units': [500, 10], 'activations': ['relu', 'softmax'], }, 'batch_norm': [False, False, False, False] }
-
ResNet for Binary classification- Just 2 values of cross-validation accuracy
I am new to python and Keras. I am trying to do a binary classification using transfer learning from ResNet. My dataset is very small but I am using image augmentation. My cross-validation accuracy is just either of 2 values 0.3442 and 0.6558 for all images. Can anyone tell me why this happens? Also when I predict (0 or 1), it labels all images as one class(0). Here is my code:
from keras.preprocessing.image import ImageDataGenerator, load_img from keras.models import Sequential,Model,load_model from keras.layers import Conv2D, MaxPooling2D from keras.layers import Activation, Dropout, Flatten, Dense, GlobalMaxPooling2D from keras import backend as K from keras.callbacks import TensorBoard from keras.applications.resnet50 import ResNet50 from keras.optimizers import SGD, Adam from keras.utils import plot_model import matplotlib.pyplot as plt import os, os.path import glob import cv2 import time from keras.utils import np_utils from keras.callbacks import ReduceLROnPlateau, CSVLogger, EarlyStopping import numpy as np import pydot import graphviz batch_size = 32 nb_classes = 2 data_augmentation = True img_rows, img_cols = 224,224 img_channels = 3 #Creating array of training samples train_path = "D:/data/train\*.*" training_data=[] for file in glob.glob(train_path): print(file) train_array= cv2.imread(file) train_array=cv2.resize(train_array,(img_rows,img_cols),3) training_data.append(train_array) x_train=np.array(training_data) #Creating array of validation samples valid_path = "D:/data/valid\*.*" valid_data=[] for file in glob.glob(valid_path): print(file) valid_array= cv2.imread(file) valid_array=cv2.resize(valid_array,(img_rows,img_cols),3) valid_data.append(train_array) x_valid=np.array(valid_data) x_train = np.array(x_train, dtype="float")/255.0 x_valid = np.array(x_valid, dtype="float")/255.0 #Creating array for Labels y_train=np.ones((num_trainsamples,1),dtype = int) y_train[0:224]=0 #Class1=0 y_train[225:363]=1 #Class2=1 print(y_train) y_valid=np.ones((num_validsamples,1),dtype = int) y_valid[0:101]=0 y_valid[102:155]=1 print(y_valid) y_train = np_utils.to_categorical(y_train, nb_classes) y_valid = np_utils.to_categorical(y_valid, nb_classes) base_model=ResNet50(weights='imagenet',include_top=False) x = base_model.output x = GlobalMaxPooling2D()(x) x=Dense(1024,activation='relu')(x) x=Dense(1024,activation='relu')(x) x=Dense(512,activation='relu')(x) x=Dense(2, activation= 'sigmoid')(x) model = Model(inputs = base_model.input, outputs = x) for i,layer in enumerate(model.layers): print(i,layer.name) for layer in model.layers[:75]: layer.trainable=False for layer in model.layers[75:]: layer.trainable=True adam = Adam(lr=0.0001) model.compile(optimizer= adam, loss='binary_crossentropy', metrics=['accuracy']) train_datagen = ImageDataGenerator( brightness_range=(0.2,2.5), rotation_range=180, zoom_range=0.5, width_shift_range=0.2, height_shift_range=0.2, horizontal_flip=True, vertical_flip=True) train_datagen.fit(x_train) history= model.fit_generator(train_datagen.flow(x_train, y_train, batch_size = 10,shuffle=True),steps_per_epoch=len(x_train),epochs = 500,shuffle=True, validation_data=(x_valid,y_valid),validation_steps=num_validsamples // batch_size,callbacks=[tensorboard]) eval = model.evaluate(x_valid, y_valid) print ("Loss = " + str(eval[0])) print ("Test Accuracy = " + str(eval[1])) predictions= model.predict(x_valid) print(predictions)
The training result is as follows:
Epoch 1/500 362/362 [==============================] - 34s 93ms/step - loss: 0.6060 - acc: 0.7257 - val_loss: 0.7747 - val_acc: 0.3442 Epoch 2/500 362/362 [==============================] - 30s 82ms/step - loss: 0.4353 - acc: 0.7722 - val_loss: 0.7658 - val_acc: 0.5000 Epoch 3/500 362/362 [==============================] - 30s 82ms/step - loss: 0.4391 - acc: 0.7863 - val_loss: 0.7949 - val_acc: 0.3442 Epoch 4/500 362/362 [==============================] - 30s 82ms/step - loss: 0.4007 - acc: 0.7992 - val_loss: 0.6540 - val_acc: 0.6558 Epoch 5/500 362/362 [==============================] - 30s 82ms/step - loss: 0.3638 - acc: 0.8226 - val_loss: 0.6460 - val_acc: 0.6558 Epoch 6/500 362/362 [==============================] - 30s 82ms/step - loss: 0.3509 - acc: 0.8294 - val_loss: 0.7875 - val_acc: 0.3442 Epoch 7/500 362/362 [==============================] - 30s 82ms/step - loss: 0.3406 - acc: 0.8359 - val_loss: 0.7667 - val_acc: 0.3442 Epoch 8/500 362/362 [==============================] - 29s 80ms/step - loss: 0.3410 - acc: 0.8365 - val_loss: 0.6900 - val_acc: 0.6558 Epoch 9/500 362/362 [==============================] - 29s 80ms/step - loss: 0.3297 - acc: 0.8366 - val_loss: 0.7292 - val_acc: 0.3442 Epoch 10/500 362/362 [==============================] - 29s 80ms/step - loss: 0.3262 - acc: 0.8412 - val_loss: 0.6829 - val_acc: 0.6558 Epoch 11/500 362/362 [==============================] - 29s 80ms/step - loss: 0.3168 - acc: 0.8457 - val_loss: 0.7032 - val_acc: 0.3442 Epoch 12/500 362/362 [==============================] - 29s 80ms/step - loss: 0.3195 - acc: 0.8452 - val_loss: 0.6985 - val_acc: 0.5000 Epoch 13/500 362/362 [==============================] - 29s 80ms/step - loss: 0.3030 - acc: 0.8432 - val_loss: 0.6740 - val_acc: 0.6558 Epoch 14/500 362/362 [==============================] - 29s 80ms/step - loss: 0.3191 - acc: 0.8405 - val_loss: 0.6896 - val_acc: 0.6558 Epoch 15/500 362/362 [==============================] - 29s 80ms/step - loss: 0.3084 - acc: 0.8437 - val_loss: 0.7114 - val_acc: 0.3442
-
keras cnn model only predicts one class for all the test images
I am trying to build an image classification model with 2 classes with (1) or without (0). I can build the model and get an accuracy of 1. which is too good to be true (which is an issue) but when I use predict_generator as I have my images in folders, it only returns 1 class 0 (without class). There seems to be an issue but I can't work it out, I have looked at a number of articles but I still can't fix the issue.
image_shape = (220, 525, 3) #height, width, channels img_width = 96 img_height = 96 channels = 3 epochs = 10 no_train_images = 11957 #!ls ../data/train/* | wc -l no_test_images = 652 #!ls ../data/test/* | wc -l no_valid_images = 6156 #!ls ../data/test/* | wc -l train_dir = '../data/train/' test_dir = '../data/test/' valid_dir = '../data/valid/' test folder structure is the following: test/test_folder/images_from_both_classes.jpg #!ls ../data/train/without/ | wc -l 5606 #theres no class inbalance #!ls ../data/train/with/ | wc -l 6351 #!ls ../data/valid/without/ | wc -l 2899 #!ls ../data/valid/with/ | wc -l 3257 classification_model = Sequential() # First layer with 2D convolution (32 filters, (3, 3) kernel size 3x3, input_shape=(img_width, img_height, channels)) classification_model.add(Conv2D(32, (3, 3), input_shape=input_shape)) # Activation Function = ReLu increases the non-linearity classification_model.add(Activation('relu')) # Max-Pooling layer with the size of the grid 2x2 classification_model.add(MaxPooling2D(pool_size=(2, 2))) # Randomly disconnets some nodes between this layer and the next classification_model.add(Dropout(0.2)) classification_model.add(Conv2D(32, (3, 3))) classification_model.add(Activation('relu')) classification_model.add(MaxPooling2D(pool_size=(2, 2))) classification_model.add(Dropout(0.2)) classification_model.add(Conv2D(64, (3, 3))) classification_model.add(Activation('relu')) classification_model.add(MaxPooling2D(pool_size=(2, 2))) classification_model.add(Dropout(0.25)) classification_model.add(Conv2D(64, (3, 3))) classification_model.add(Activation('relu')) classification_model.add(MaxPooling2D(pool_size=(2, 2))) classification_model.add(Dropout(0.3)) classification_model.add(Flatten()) classification_model.add(Dense(64)) classification_model.add(Activation('relu')) classification_model.add(Dropout(0.5)) classification_model.add(Dense(1)) classification_model.add(Activation('sigmoid')) # Using binary_crossentropy as we only have 2 classes classification_model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy']) batch_size = 32 # this is the augmentation configuration we will use for training train_datagen = ImageDataGenerator( rescale=1. / 255, zoom_range=0.2) # this is the augmentation configuration we will use for testing: # only rescaling valid_datagen = ImageDataGenerator(rescale=1. / 255) test_datagen = ImageDataGenerator() train_generator = train_datagen.flow_from_directory( train_dir, target_size = (img_width, img_height), batch_size = batch_size, class_mode = 'binary', shuffle = True) valid_generator = valid_datagen.flow_from_directory( valid_dir, target_size = (img_width, img_height), batch_size = batch_size, class_mode = 'binary', shuffle = False) test_generator = test_datagen.flow_from_directory( test_dir, target_size = (img_width, img_height), batch_size = 1, class_mode = None, shuffle = False) mpd = classification_model.fit_generator( train_generator, steps_per_epoch = no_train_images // batch_size, # number of images per epoch epochs = epochs, # number of iterations over the entire data validation_data = valid_generator, validation_steps = no_valid_images // batch_size)
Epoch 1/10 373/373 [==============================] - 119s 320ms/step - loss: 0.5214 - acc: 0.7357 - val_loss: 0.2720 - val_acc: 0.8758
Epoch 2/10 373/373 [==============================] - 120s 322ms/step - loss: 0.2485 - acc: 0.8935 - val_loss: 0.0568 - val_acc: 0.9829
Epoch 3/10 373/373 [==============================] - 130s 350ms/step - loss: 0.1427 - acc: 0.9435 - val_loss: 0.0410 - val_acc: 0.9796
Epoch 4/10 373/373 [==============================] - 127s 341ms/step - loss: 0.1053 - acc: 0.9623 - val_loss: 0.0197 - val_acc: 0.9971
Epoch 5/10 373/373 [==============================] - 126s 337ms/step - loss: 0.0817 - acc: 0.9682 - val_loss: 0.0136 - val_acc: 0.9948
Epoch 6/10 373/373 [==============================] - 123s 329ms/step - loss: 0.0665 - acc: 0.9754 - val_loss: 0.0116 - val_acc: 0.9985
Epoch 7/10 373/373 [==============================] - 140s 376ms/step - loss: 0.0518 - acc: 0.9817 - val_loss: 0.0035 - val_acc: 0.9997
Epoch 8/10 373/373 [==============================] - 144s 386ms/step - loss: 0.0539 - acc: 0.9832 - val_loss: 8.9459e-04 - val_acc: 1.0000
Epoch 9/10 373/373 [==============================] - 122s 327ms/step - loss: 0.0434 - acc: 0.9850 - val_loss: 0.0023 - val_acc: 0.9997
Epoch 10/10 373/373 [==============================] - 125s 336ms/step - loss: 0.0513 - acc: 0.9844 - val_loss: 0.0014 - val_acc: 1.0000
valid_generator.batch_size=1 score = classification_model.evaluate_generator(valid_generator, no_test_images/batch_size, pickle_safe=False) test_generator.reset() scores=classification_model.predict_generator(test_generator, len(test_generator)) print("Loss: ", score[0], "Accuracy: ", score[1]) predicted_class_indices=np.argmax(scores,axis=1) print(predicted_class_indices) labels = (train_generator.class_indices) labelss = dict((v,k) for k,v in labels.items()) predictions = [labelss[k] for k in predicted_class_indices] filenames=test_generator.filenames results=pd.DataFrame({"Filename":filenames, "Predictions":predictions}) print(results)
Loss: 5.404246180551993e-06 Accuracy: 1.0
print(predicted_class_indices) - ALL 0
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
Filename Predictions 0 test_folder/video_3_frame10.jpg without 1 test_folder/video_3_frame1001.jpg without 2 test_folder/video_3_frame1006.jpg without 3 test_folder/video_3_frame1008.jpg without 4 test_folder/video_3_frame1009.jpg without 5 test_folder/video_3_frame1010.jpg without 6 test_folder/video_3_frame1013.jpg without 7 test_folder/video_3_frame1014.jpg without 8 test_folder/video_3_frame1022.jpg without 9 test_folder/video_3_frame1023.jpg without 10 test_folder/video_3_frame103.jpg without 11 test_folder/video_3_frame1036.jpg without 12 test_folder/video_3_frame1039.jpg without 13 test_folder/video_3_frame104.jpg without 14 test_folder/video_3_frame1042.jpg without 15 test_folder/video_3_frame1043.jpg without 16 test_folder/video_3_frame1048.jpg without 17 test_folder/video_3_frame105.jpg without 18 test_folder/video_3_frame1051.jpg without 19 test_folder/video_3_frame1052.jpg without 20 test_folder/video_3_frame1054.jpg without 21 test_folder/video_3_frame1055.jpg without 22 test_folder/video_3_frame1057.jpg without 23 test_folder/video_3_frame1059.jpg without 24 test_folder/video_3_frame1060.jpg without
...just some of the outputs but all 650+ are without class.
This is the output and as you can see all the predicted values are 0 for the without class.
This is my first attempt at using Keras and CNN so any help would be really appreciated.
-
How to return a multidimensional Mat array from a function
I want to return a cv::Mat object with a [w][h][c] dimensions, where w is width, h is height and c are channels or depth. But the compiler gives me an error.
This is my code:
cv::Mat three_channel_convolution(cv::Mat bgr_image, cv::Mat kernel) { cv::Mat feature_map[3]; cv::Mat bgr[3]; cv::split(bgr_image, bgr); for (uchar i = 0; i < 3; i++) { feature_map[i] = one_channel_convolution(bgr[i], kernel); std::string name_feature_map = "bgr_feature_map_" + std::to_string(i) + ".jpg"; cv::imwrite(name_feature_map, feature_map[i]); } return feature_map; }
In this function I want to make a convolution with a filter (kernel) for each of the three channels in the image BGR. So the convolution function returns the result of the operation in one channel (A single 2D Mat [][]) and stores it in the feature_map Mat array. How can I return this Mat array?
Then in my main() function I call the function like this:
feature_map = three_channel_convolution(image, kernels[0]);
Thanks for the help!
-
OpenCVForUnity always initialize empty matrices
I am working on a project in Unity, using the package OpenCVForUnity. When I try to initialize a matrix (Mat) with given rows and columns, there is something that fails and the program returns an error.
The following program is meant to use the Unity built-in Video Player and then pass each frames to OpenCV Mat, by means of the OpenCVForUnity plugin.
Here is the code:
using UnityEngine; using UnityEngine.UI; using UnityEngine.Video; using System.Collections; using System.Linq; /* * https://enoxsoftware.com/opencvforunity/mat-basic-processing1/ */ #if UNITY_5_3 || UNITY_5_3_OR_NEWER using UnityEngine.SceneManagement; #endif using OpenCVForUnity; public class myHOG3 : MonoBehaviour { VideoPlayer videoPlayer; Mat rgbaMat; Color32[] colors; Texture2D texture; Texture2D videoTexture; // Use this for initialization void Start () { videoPlayer = GetComponent<VideoPlayer> (); int width = (int)videoPlayer.clip.width; int height = (int)videoPlayer.clip.height; colors = new Color32[width * height]; texture = new Texture2D (width, height, TextureFormat.RGBA32, false); rgbaMat = new Mat (height, width, CvType.CV_64FC1); Debug.Log("matrix width = " + rgbaMat.cols() + " / matrix height = " + rgbaMat.rows()); videoTexture = new Texture2D (width, height, TextureFormat.RGBA32, false); videoPlayer.Play (); gameObject.GetComponent<Renderer> ().material.mainTexture = texture; } // Update is called once per frame void Update () { if (videoPlayer.isPlaying && videoPlayer.texture != null) { Utils.textureToTexture2D (videoPlayer.texture, videoTexture); Utils.fastTexture2DToMat (videoTexture, rgbaMat); Imgproc.putText (rgbaMat, "VideoPlayer With OpenCV for Unity Example", new Point (100, rgbaMat.rows () / 2), 1, 1.5, new Scalar (255, 0, 0, 255), 5, Imgproc.LINE_AA, false); Imgproc.putText (rgbaMat, "width:" + rgbaMat.width () + " height:" + rgbaMat.height () + " frame:" + videoPlayer.frame, new Point (5, rgbaMat.rows () - 10), 1, 1.5, new Scalar (255, 255, 255, 255), 5, Imgproc.LINE_AA, false); Utils.fastMatToTexture2D (rgbaMat, texture); } } }
As you can see I am initializing the matrix with height and width:
rgbaMat = new Mat (height, width, CvType.CV_64FC1);
but when I run the program, the debug control on the matrix height and width always returns 0.
Debug.Log("matrix width = " + rgbaMat.cols() + " / matrix height = " + rgbaMat.rows());
This is a problem, because when I call the method
fastTexture2DToMat
the compiler returns an error which states:ArgumentException: The output Mat object has to be of the same size
Software version:
- Unity 2017.4.13 (LTS) Personal
- OpenCVForUnity 2.1.6
-
Modify Mat object Pixel values by using OpenCV
I'm a beginner in Android Programming.I'm building an Android App for Image Steganography.I already developed this application for Desktop using Java and OpenCV,which works Perfectly.I'm trying to re-use the code,which I have.
I created a
Mat
Object and passed it to the Java Class(which I already have).I'm able to read the Pixel Intensities in theMat
Object,But,when I'm trying to modify those values (by usingput()
),my app is getting crashed.The Exception thrown was
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.hari.imagesteganography, PID: 8850 java.lang.UnsupportedOperationException: Provided data element number (3) should be multiple of the Mat channels count (4) at org.opencv.core.Mat.put(Mat.java:954) at com.example.hari.imagesteganography.LSBImageStego.encodeOriginalMessageBinaryLength(LSBImageStego.java:101) at com.example.hari.imagesteganography.LSBImageStego.encodeImage(LSBImageStego.java:239) at com.example.hari.imagesteganography.EncodeActivity$1.onClick(EncodeActivity.java:98) at android.view.View.performClick(View.java:6302) at android.view.View$PerformClick.run(View.java:24782) at android.os.Handler.handleCallback(Handler.java:790) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6518) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
The same method (
put()
) works perfectly fine running on Desktop.Following is my Code in the Android application.
private void encodeOriginalMessageBinaryLength(){ // LOGGING THIS,SHOWS THAT THE FUNCTION EXECUTION STARTED Log.i("URI" , "encodeOriginalMessageBinaryLength()"); String binary_of_originalMessageBinaryLength = Integer.toBinaryString(this.originalMessageBinaryLength); if(binary_of_originalMessageBinaryLength.length()%2 !=0){ binary_of_originalMessageBinaryLength = "0" + binary_of_originalMessageBinaryLength; } int remaining = binary_of_originalMessageBinaryLength.length(); String newLsbBits; for(int col = this.coverImage_cols -1 ; col >=0 ; col--){ if(remaining > 0){ newLsbBits = binary_of_originalMessageBinaryLength.substring( remaining - NUMBER_OF_BITS_REPLACING , remaining ); remaining -= NUMBER_OF_BITS_REPLACING; }else{ newLsbBits = String.join("", Collections.nCopies(NUMBER_OF_BITS_REPLACING, "0")); } Log.i("URI","newLsbBits"+newLsbBits); String modifiedBinaryString = this.getBeautifiedBinaryString(this.coverImage.get( this.coverImage_rows -1,col )[0]).substring(0 , this.getBeautifiedBinaryString(this.coverImage.get( this.coverImage_rows -1,col )[0]).length() - NUMBER_OF_BITS_REPLACING) + newLsbBits; Log.i("URI","modifiedBianryString"+modifiedBinaryString); double[] data = new double[3]; data[0] = Integer.parseInt(modifiedBinaryString , 2); data[1] = this.coverImage.get( this.coverImage_rows -1,col )[1]; data[2] = this.coverImage.get( this.coverImage_rows -1,col )[2]; this.coverImage_rows -1,col )[0]); // LOGGING THE DIMENSIONS OF THE IMAGE CORRECTLY,SHOWS THAT THE Mat Object // IS INITIALIZED CORRECTLY Log.i("URI" , new Integer(this.coverImage_rows).toString()); Log.i("URI" , new Integer(this.coverImage_cols).toString()); // EXECUTING BELOW LINE CAUSES THE APP TO CRASH this.coverImage.put( this.coverImage_rows -1,col , data); // BELOW MESSAGE IS NOT BEING LOGGED Log.i("URI" , "encodeOriginalMessageBinaryLength() END"); } }
The Corresponding Logcat log is as follows.
2019-02-08 12:30:37.019 1230-1230/com.example.hari.imagesteganography I/URI: encodeOriginalMessageBinaryLength() 2019-02-08 12:30:37.019 1230-1230/com.example.hari.imagesteganography I/URI: newLsbBits10 2019-02-08 12:30:37.019 1230-1230/com.example.hari.imagesteganography I/URI: modifiedBianryString10010110 2019-02-08 12:30:37.019 1230-1230/com.example.hari.imagesteganography I/URI: 1440 2019-02-08 12:30:37.019 1230-1230/com.example.hari.imagesteganography I/URI: 2560
The
Mat
Object and other attributes are being set as followspublic LSBImageStego(Mat coverImage){ this.coverImage = coverImage; this.coverImage_rows = (int)coverImage.size().height; this.coverImage_cols = (int)coverImage.size().width; }
Is my code wrong?How to change Pixel Intensity values of
Mat
Object using OpenCV and Java in Android?I believe that,this question is not duplicate of Using get() and put() to access pixel values in OpenCV for Java OR opencv java modify pixel values ,as they are not intended for Android, and the
put()
method described in those questions,works fine even for me on Desktop. -
How data pass for previous view controller using protocol and update collectionview cell swift
First view controller is Collectionview cell and pass data to nextview controller. Nextviewcontroller Edit the text and pass the text in first view contoller-collectionview cell and update the collectionview cell.
-
Could realtime EEG signal used directly in LSTM?
I am doing an experiment that using raw EEG data for LSTM. I have some quenstion about the input shape of this networks. Some one says that "You need a feature dimension, a batch size dimension and a time dimension". I think that means the input shape should be (feature,batch,time)。 But now I want to use the raw data without feature extraction.I think it may be unfitted for the raw data(256Hz/s).
For example,I have a long data for an hour(921600points). If I seperated in time-domian, each slice have 512 points. Can I use the 512 points as the directly input as the feature dimension?
-
Meaning: improvement in RMSE during crossvalidation, although not on testset?
In the code below I train a NN with crossvalidation on the first 20000 records in the dataset. The dataset contains 8 predictors.
First I have split my data in 2 parts: the first 20.000 rows (trainset) and the last 4003 rows (out of sample test set)
I have done 2 runs: run 1) a run with 3 predictors run 2) a run with all 8 predictors (see code below).
Based on crossvalidation within the 20.000 rows from the trainset, the RMSE (for the optimal parametersetting) improves from 2.30 (run 1) to 2.11 (run 2).
Although when I test both models on the 4003 rows from the out of sample test set, the RMSE improves only neglible from 2.64 (run 1) to 2.63 (run 2).
What can be concluded from these contradiction in the results?
Thanks!
### R code from Applied Predictive Modeling (2013) by Kuhn and Johnson. ### Chapter 7: Non-Linear Regression Models ### Required packages: AppliedPredictiveModeling, caret, doMC (optional), ### earth, kernlab, lattice, nnet ################################################################################ library(caret) ### Load the data mydata <- read.csv(file="data.csv", header=TRUE, sep=",") validatiex <- mydata[20001:24003,c(1:8)] validatiey <- mydata[20001:24003,9] mydata <- mydata[1:20000,] x <- mydata[,c(1:8)] y <- mydata[,9] parti <- createDataPartition(y, times = 1, p=0.8, list = FALSE) x_train <- x[parti,] x_test <- x[-parti,] y_train <- y[parti] y_test <- y[-parti] set.seed(100) indx <- createFolds(y_train, returnTrain = TRUE) ctrl <- trainControl(method = "cv", index = indx) ## train neural net: nnetGrid <- expand.grid(decay = c(.1), size = c(5, 15, 30), bag = FALSE) set.seed(100) nnetTune <- train(x = x_train, y = y_train, method = "avNNet", tuneGrid = nnetGrid, trControl = ctrl, preProc = c("center", "scale"), linout = TRUE, trace = FALSE, MaxNWts = 30 * (ncol(x_train) + 1) + 30 + 1, maxit = 1000, repeats = 25, allowParallel = FALSE) nnetTune plot(nnetTune) predictions <- predict(nnetTune, validatiex, type="raw") mse <- mean((validatiey - predictions)^2) mse <- sqrt(mse) print (mse)