writing a image recognition program with my own inputs
I'm trying to create an image recognition program with the dataset as my own images. I only have 506 images (this program is basically a proof of concept). I'm using tensorflow and I'm stuck when it comes to feeding my images and labels.
Tensorflow has this function
batch_xs, batch_ys = mnist.train.next_batch(100)
but I can't use that for my own inputs. I've tried train_test_split, but when running, I get a value error stating
"Cannot feed value of shape (379, 50, 50, 1) for Tensor 'Placeholder:0', which has shape '(?, 2500)'".
I'm not sure where the 379 is coming from. Any suggestions? Here's my code:
x = tf.placeholder(tf.float32, [None, 2500])
W = tf.Variable(tf.zeros([2500, 3]))
b = tf.Variable(tf.zeros([3]))
y = tf.nn.softmax(tf.matmul(x, W) + b)
y_ = tf.placeholder(tf.float32, [None, 3])
cross_entropy = tf.reduce_mean(tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
sess = tf.InteractiveSession()
tf.global_variables_initializer().run()
batch_xs, test_x, batch_ys, test__y = train_test_split(img, index, test_size = .25, random_state = 0)
sess.run(train_step, feed_dict= {x: batch_xs, y_: batch_ys})
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
#figure out how to get xs and ys into this statement
print(sess.run(accuracy, feed_dict= {x: batch_xs, y_: batch_ys}))
See also questions close to this topic

Why does python add a new line at the end of my strings?
I have a small question. I noticed that, for some reason, when I use the "+" symbol when joining two variables, Python automatically uses a newline.
for i in range(o): a = Before.readline() b = After.readline() if a == b: lines.append(" \n") else: plus = a + b lines.append(a + b) Final.writelines(lines)
This would result in a list with values as such (Notice the
'B\nC\n'
)[' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', 'B\nC\n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n', ' \n'] Process finished with exit code 0
Assuming that I have initialized the files "Before", "After", and "Final" correctly, what should I do to remove these newlines and just add a space? I would prefer to do this without using many libraries.
Thanks in advance!

Python variables not getting garbage collected causing C++ process memory to increase
I have a Python script like this:
import module globalvariable a def read(key): x = funcA(a, key) ret = funcB(x) if ret == True: return "Success" else: return "Failure"
I am calling this function from my C++ program repeatedly from time to time via the PythonCAPI. I noticed that my C++ process memory kept increasing each time I called this function.
After a lot of investigation by:
 Running Valgrind
 Checking all the Py_DECREFs of all the variables/PyObjects in my C++ code
 Ensuring my code is not leaking memory elsewhere
 Replaced my raw PythonCAPI with boost python and checked again
I have found out that the C++ memory is increasing because of the Python script itself and not the PythonCAPI. I confirmed this by replacing the above script with a dummy script(that has no variables and doesn't do anything) and calling the same function repeatedly. The process memory was constant and did not increase this time.
How do I force the Python Garbage collector to free all the variables after each function call? I don't know if/when the Python GC will free memory. I noticed some suggestions in other stackoverflow answers that forcing the Garbage collector to collect is a bad thing to do.
How do I ensure that no additional memory is getting used up by my c++ process each time I call the python script? Is it as simple as calling a del() on all the local python variables that I declare? Or is there some other way?

Function sum of square python
Im trying to code the sum of squares in python and i'm pretty new to this
this is what i have for now,
n=int(input("n=")) def sumsquare(n): sum=0 i=0 while(n<=i): sum= sum + i**2
Basically what i'm trying to do is to make the user choose a number, and based on that number I want to calculate the sum of squares , and return "The sum of square is ___"
Thanks in advance for your help !

How can maxpooling only get the max , but not to resize the featuremap, in numpy, pytorch or tensorflow
I want to implement a max pooling in such way: the nodes that is not the max only become zero , so the featuremap remain the same size after pooling. Maybe this operation is write in C , but i think it's also possible in numpy?

Training DeepLab on custom data without a pretrained model
I was able to run
DeepLab V3
on my own dataset, but would like to ask on how I can do so without the pretrained model.Thanks a lot.

Keras Graph disconnects when combining a LSTM with a CNN
I try to get the following code working, but it is failing giving the error message:
Graph disconnected: cannot obtain value for tensor Tensor("bidirectional_4/concat:0", shape=(?, ?, 100), dtype=float32) at layer "input_11". The following previous layers were accessed without issue: []
The code to train the model is the following:
def train_model(self, sentences_pair, is_similar, embedding_meta_data_skt, embedding_meta_data_tib , model_save_directory='./'): tokenizer_skt = embedding_meta_data_skt['tokenizer'] tokenizer_tib = embedding_meta_data_tib['tokenizer'] embedding_matrix_skt = embedding_meta_data_skt['embedding_matrix'] embedding_matrix_tib = embedding_meta_data_tib['embedding_matrix'] train_data_x1, train_data_x2, train_labels, leaks_train, \ val_data_x1, val_data_x2, val_labels, leaks_val = create_train_dev_set(tokenizer_skt, sentences_pair, is_similar, self.max_sequence_length, self.validation_split_ratio) nb_words_skt = len(tokenizer_skt.word_index) + 1 nb_words_tib = len(tokenizer_tib.word_index) + 1 # Creating word embedding layer embedding_layer_skt = Embedding(nb_words_skt, self.embedding_dim, weights=[embedding_matrix_skt], input_length=self.max_sequence_length, trainable=False) embedding_layer_tib = Embedding(nb_words_tib, self.embedding_dim, weights=[embedding_matrix_tib], input_length=self.max_sequence_length, trainable=False) # Creating LSTM Encoder lstm_layer = Bidirectional(LSTM(self.number_lstm_units, dropout=self.rate_drop_lstm, recurrent_dropout=self.rate_drop_lstm,return_sequences=True)) # Creating LSTM Encoder layer for First Sentence sequence_1_input = Input(shape=(self.max_sequence_length,), dtype='int32') embedded_sequences_1 = embedding_layer_skt(sequence_1_input) skt_lstm = lstm_layer(embedded_sequences_1) # Creating LSTM Encoder layer for Second Sentence sequence_2_input = Input(shape=(self.max_sequence_length,), dtype='int32') embedded_sequences_2 = embedding_layer_tib(sequence_2_input) tib_lstm = lstm_layer(embedded_sequences_2) A_input = keras.Input(tensor=skt_lstm) B_input = keras.Input(tensor=tib_lstm) dist_output = keras.layers.Lambda(pairwise_cosine_sim)([skt_lstm,tib_lstm,A_input,B_input]) dist_output = Reshape((40,40,1))(dist_output) input_shape = (40,40,1) cnn_model = Conv2D(128, (2, 2), input_shape=input_shape)(dist_output) cnn_model = BatchNormalization(axis=1)(cnn_model) cnn_model = Activation('relu')(cnn_model) cnn_model = Conv2D(164, (2, 2))(cnn_model) cnn_model = BatchNormalization(axis=1)(cnn_model) cnn_model = Activation('relu')(cnn_model) cnn_model = Conv2D(192,(3, 3))(cnn_model) cnn_model = BatchNormalization(axis=1)(cnn_model) cnn_model = Activation('relu')(cnn_model) cnn_model = Conv2D(192, (3, 3))(cnn_model) cnn_model = BatchNormalization(axis=1)(cnn_model) cnn_model = Activation('relu')(cnn_model) cnn_model = Conv2D(128, (3, 3))(cnn_model) cnn_model = BatchNormalization(axis=1)(cnn_model) cnn_model = Activation('relu')(cnn_model) cnn_model = MaxPooling2D(pool_size=(2,2))(cnn_model) cnn_model = Dropout(0.40)(cnn_model) cnn_model = Flatten()(cnn_model) # Fully connected layer cnn_model = Dense(256)(cnn_model) cnn_model = BatchNormalization()(cnn_model) cnn_model = Activation('relu')(cnn_model) cnn_model = Dropout(0.5)(cnn_model) cnn_model = Dense(num_classes)(cnn_model) preds = Dense(1, activation='sigmoid')(cnn_model) model = Model(inputs=[sequence_1_input, sequence_2_input], outputs=preds) model.compile(loss=keras.losses.binary_crossentropy, optimizer=keras.optimizers.Adam(lr=learning_rate), metrics=['accuracy']) #model.compile(loss='binary_crossentropy', optimizer='nadam', metrics=['acc']) filepath="skttibbs" + str(batch_size) + "" + "{epoch:02d}{val_acc:.2f}.hdf5" checkpoint = ModelCheckpoint('skttib.h5', monitor='val_acc') callbacks_list = [checkpoint] model.fit([train_data_x1, train_data_x2, leaks_train], train_labels,validation_data=([val_data_x1, val_data_x2, leaks_val], val_labels), batch_size=batch_size, epochs=epochs, verbose=1, class_weight = class_weight, callbacks = callbacks_list) score = model.evaluate(x_test, y_test, verbose=1) print('Test loss:', score[0]) print('Test accuracy:', score[1]) model.save(file_name)
I Have been trying for a couple of hours to fix it, but it seems to be no good. Somewhere the input and outputs are not connected, but I just can't figure out where the problem lies. Any suggestions on this?

What happens if we resize images repeatedly
I need a dataset(image). So I downloaded images, for training purpose I resized images twice. From random sizes to (200,300), using the resized images I resized them to (64,64). Will I face a problem while training. Does a picture loss it''s data when resized. Any help is appreciated. Thank you.

Why using pretrained ResNet50 results in contradictory losses in training and validation set?
I'm using ResNet50 pretrained model as building block for a Unet:
def ResNet50(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000): if weights not in {'imagenet', None}: raise ValueError('The `weights` argument should be either ' '`None` (random initialization) or `imagenet` ' '(pretraining on ImageNet).') if weights == 'imagenet' and include_top and classes != 1000: raise ValueError('If using `weights` as imagenet with `include_top`' ' as true, `classes` should be 1000') if input_tensor is None: img_input = Input(shape=input_shape) else: if not K.is_keras_tensor(input_tensor): img_input = Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor if K.image_data_format() == 'channels_last': bn_axis = 3 else: bn_axis = 1 x = Conv2D(64, (7, 7), strides=(2, 2), padding='same', name='conv1')(img_input) x = BatchNormalization(axis=bn_axis, name='bn_conv1')(x) x = Activation('relu')(x) x = MaxPooling2D((3, 3), strides=(2, 2), padding="same")(x) x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1)) x = identity_block(x, 3, [64, 64, 256], stage=2, block='b') x = identity_block(x, 3, [64, 64, 256], stage=2, block='c') x = conv_block(x, 3, [128, 128, 512], stage=3, block='a') x = identity_block(x, 3, [128, 128, 512], stage=3, block='b') x = identity_block(x, 3, [128, 128, 512], stage=3, block='c') x = identity_block(x, 3, [128, 128, 512], stage=3, block='d') x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e') x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f') x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a') x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b') x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c') # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input # Create model. model = Model(inputs, x, name='resnet50') # load weights if weights == 'imagenet': if include_top: weights_path = get_file('resnet50_weights_tf_dim_ordering_tf_kernels.h5', WEIGHTS_PATH, cache_subdir='models', md5_hash='a7b3fe01876f51b976af0dea6bc144eb') else: weights_path = get_file('resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5', WEIGHTS_PATH_NO_TOP, cache_subdir='models', md5_hash='a268eb855778b3df3c7506639542a6af') model.load_weights(weights_path,by_name=True) return model
Creating the Unet:
def conv_block_simple(prevlayer, filters, prefix, strides=(1, 1)): conv = Conv2D(filters, (3, 3), padding="same", kernel_initializer="he_normal", strides=strides, name=prefix + "_conv")(prevlayer) conv = BatchNormalization(name=prefix + "_bn")(conv) conv = Activation('relu', name=prefix + "_activation")(conv) return conv def conv_block_simple_no_bn(prevlayer, filters, prefix, strides=(1, 1)): conv = Conv2D(filters, (3, 3), padding="same", kernel_initializer="he_normal", strides=strides, name=prefix + "_conv")(prevlayer) conv = Activation('relu', name=prefix + "_activation")(conv) return conv K.clear_session() def get_unet_resnet(input_shape): resnet_base = ResNet50(input_shape=input_shape, include_top=False) for l in resnet_base.layers: l.trainable = False conv1 = resnet_base.get_layer("activation_1").output conv2 = resnet_base.get_layer("activation_10").output conv3 = resnet_base.get_layer("activation_22").output conv4 = resnet_base.get_layer("activation_40").output conv5 = resnet_base.get_layer("activation_49").output up6 = concatenate([UpSampling2D()(conv5), conv4], axis=1) conv6 = conv_block_simple(up6, 256, "conv6_1") conv6 = conv_block_simple(conv6, 256, "conv6_2") up7 = concatenate([UpSampling2D()(conv6), conv3], axis=1) conv7 = conv_block_simple(up7, 192, "conv7_1") conv7 = conv_block_simple(conv7, 192, "conv7_2") up8 = concatenate([UpSampling2D()(conv7), conv2], axis=1) conv8 = conv_block_simple(up8, 128, "conv8_1") conv8 = conv_block_simple(conv8, 128, "conv8_2") up9 = concatenate([UpSampling2D()(conv8), conv1], axis=1) conv9 = conv_block_simple(up9, 64, "conv9_1") conv9 = conv_block_simple(conv9, 64, "conv9_2") up10 = UpSampling2D()(conv9) conv10 = conv_block_simple(up10, 32, "conv10_1") conv10 = conv_block_simple(conv10, 32, "conv10_2") conv10 = SpatialDropout2D(0.2)(conv10) x = Conv2D(1, (1, 1), activation="sigmoid", name="prediction")(conv10) model = Model(resnet_base.input, x) model.summary() return model
I freezed the pretrained ResNet50 layers as proposed in several papers:
for l in resnet_base.layers: l.trainable = False
Without the freezing the network works fine, but tends to hugely overfit, which I decreased with higher
SpatialDropout2D()
value. When I freeze it however the train losses decrease but the validation losses circulate about some weird high value but actually stagnate. I can't figure out, why the frozen network do work on training set, meanwhile it doesn't on validation set. I see no logical reason for it (on my current knowledge level).I tried playing with learning rate, but no success.
What could be the problem? Any help would be highly appreciated. Thank you.

how to build and train a traffic light and traffic signs recognizer
I'm a beginner in opencv and in computer vision in general. I'm in the fourth year of secondary school and my teacher gave me this project to do : Create a program that can recognize from a camera traffic lights and traffics signs. I started searching for some examples of image classifier in the internet and I found this example : https://www.pyimagesearch.com/2017/09/18/realtimeobjectdetectionwithdeeplearningandopencv/ .
It works well but the problem is that it doesn't do what i need. My main problem is: how can I train a caffemodel for this program? or better, did you know any guide to create our own dataset and then create our own image classifier with opencv?

How to verify the shape drawn by user in Unity
I am creating an app in Unity, where the user will be presented by alphabets images and user will be able to trace the character in the image using his finger. I know how to do the tracing part in Unity but what I don't know is how to verify whether the character drawn by the user is correct or not.
For eg. User is presented with an image of character A. Now instead of drawing A user make character B. Now how can I verify that the user has drawn the wrong character and ask him to try again.
How can I achieve the above functionality? I don't have any code for this as don't have any idea how to progress with this.

Do pixel attacks require the network to use gradient descent to work?
Lately, I have been reading about pixel attacks and how easily neural networks can be tricked. From my understanding, the attacks work by exploiting gradient descent by presenting a closer local min point for the algorithm to settle.
I was wondering if pixel attacks are also applicable to other techniques like SGD or Adam? If so how is that possible since they can find the global minimum point. Is the only way to trick them to actually change the image to a great degree or is there another way?