How do I load pretrained weight as filter that process between layers
I now have built convolutional layers and maxpooling layers. I only want to do forward propagation, so I decided to use the weights which already been trained as filters.
How am I able to load the weights into my model as the shape to be (batch_size, height, width, channel).
There are weights from other models such as YOLOV3.weights
I just need to load them and apply them into my layers
How do I do that?
See also questions close to this topic

Keras LSTM different X timesteps to Y timesteps (e.g. learn on last 4 predict next 2)
I'm having some trouble getting an LSTM model in Keras to train on the last 4 timesteps and then just predict the next 2 timesteps.
I'm sure its possible but think I'm just confusing some of the keras api.
Here is a Google Colab workbook that generates some fake data, reshapes the X and Y to be passed into the model and then trains the model.
If I set
X_N_TIMESTEPS
to be the same asY_N_TIMESTEPS
it trains fine  so for example use the last 4 timesteps to predict the next 4.But I'm trying to be a bit more general and be able to train on say last 4 timesteps and then predict the next 2. The
make_xy()
function reshapes the data as I think it needs to. e.g.X.shape=(1995, 4, 3) Y.shape=(1995, 2, 3)
I think what I'm missing is telling the last
Dense()
layer I want it to output just 2 timesteps. The error I get is:ValueError: Error when checking target: expected dense_1 to have shape (4, 3) but got array with shape (2, 3)
Which sort of suggests the last dense layer does not know I just want 2 timesteps even though that's what I'm passing in as the Y values.
I found this which indicated that maybe I could pass an output_dim to the last dense layer but I get an error if I try that saying I need to use keras api v2, and when I look at the docs for Dense I think the api must have changed a bit since then.
Here is all the code (in case its preferred over the colab link):
import numpy as np import pandas as pd from numpy import concatenate from matplotlib import pyplot from keras.models import Sequential from keras.callbacks import Callback from keras.layers import LSTM, Dense, Activation import matplotlib.pyplot as plt # %matplotlib inline # define some variables N_FEATURES = 3 X_N_TIMESTEPS = 4 Y_N_TIMESTEPS = 2 N_DATA_ORIG = 3000 N_ROLLING = 1000 N_DATA = N_DATA_ORIG  N_ROLLING # make some noisy but smooth looking data data = np.sqrt(np.random.rand(N_DATA_ORIG,N_FEATURES)) df_data = pd.DataFrame(data) df_data = df_data.rolling(window=N_ROLLING).mean() df_data = df_data.dropna() df_data = df_data.head(N_DATA) print(df_data.shape) data = df_data.values print(data.shape) print(df_data.head()) # plot the normal healthy data fig, ax = plt.subplots(num=None, figsize=(14, 6), dpi=80, facecolor='w', edgecolor='k') size = len(data) for x in range(data.shape[1]): ax.plot(range(0,size), data[:,x], '', linewidth=1) def make_xy(data,x_n_timesteps,y_n_timesteps,print_info=True): ''' Function to reshape the data into model ready format, either for training or prediction. ''' # get original data shape data_shape = data.shape # get n_features from shape of input data n_features = data_shape[1] # loop though each row of data and reshape accordingly for i in range(len(data)): # row to start on for x xi = i # row to start on for y yi = i + x_n_timesteps x = np.array([data[i:(i+x_n_timesteps),]]) y = np.array([data[yi:(yi+y_n_timesteps),]]) # only collect valid shapes if (x.shape == (1,x_n_timesteps,n_features)) & (y.shape == (1,y_n_timesteps,n_features)): # if initial data then copy else concatenate if i == 0: X = x Y = y else: X = np.concatenate((X,x)) Y = np.concatenate((Y,y)) if print_info: print('X.shape={}'.format(X.shape)) print('Y.shape={}'.format(Y.shape)) return X, Y # build network model = Sequential() model.add(LSTM(10,input_shape=(X_N_TIMESTEPS,N_FEATURES),return_sequences=True)) model.add(LSTM(10,return_sequences=True)) model.add(Dense(N_FEATURES)) model.compile(loss='mae', optimizer='adam') # print model summary print(model.summary()) # reshape data for training print(f'... reshaping data for training ...') X, Y = make_xy(data,X_N_TIMESTEPS,Y_N_TIMESTEPS) # fit model model.fit(X, Y)

How plot epoch vs val_acc and epoch vs val_loss graph in CNN?
I have used CNN for traing a datasets. Here,i get epoch,val_loss,val_acc,total loss, traning time etc. as a history. If i want to calculate the average of accuracy,then how to access the val_acc? and How to plot the epoch vs val_acc and epoch vs val_loss graph??
convnet = input_data(shape=[None, IMG_SIZE, IMG_SIZE, 3], name='input') convnet = conv_2d(convnet, 32, 3, activation='relu') convnet = max_pool_2d(convnet, 3) convnet = conv_2d(convnet, 64, 3, activation='relu') convnet = max_pool_2d(convnet, 3) convnet = conv_2d(convnet, 128, 3, activation='relu') convnet = max_pool_2d(convnet, 3) convnet = conv_2d(convnet, 32, 3, activation='relu') convnet = max_pool_2d(convnet, 3) convnet = conv_2d(convnet, 64, 3, activation='relu') convnet = max_pool_2d(convnet, 3) convnet = fully_connected(convnet, 1024, activation='relu') convnet = dropout(convnet, 0.8) convnet = fully_connected(convnet, 4, activation='softmax') convnet = regression(convnet, optimizer='adam', learning_rate=LR, loss='categorical_crossentropy', name='targets') model = tflearn.DNN(convnet, tensorboard_dir='log') if os.path.exists('{}.meta'.format(MODEL_NAME)): model.load(MODEL_NAME) print('model loaded!') train = train_data[:150] test = train_data[50:] X = np.array([i[0] for i in train]).reshape(1,IMG_SIZE,IMG_SIZE,3) Y = [i[1] for i in train] test_x = np.array([i[0] for i in test]).reshape(1,IMG_SIZE,IMG_SIZE,3) test_y = [i[1] for i in test] hist=model.fit({'input': X}, {'targets': Y}, n_epoch=8, validation_set=({'input': test_x}, {'targets': test_y}), snapshot_step=40, show_metric=True, run_id=MODEL_NAME) model.save(MODEL_NAME)

Restore Model Error in Tensorflow using Jupyter Notebook
After model is trained in tensorflow, I want to upload the trained model using saver class. This code makes error or runs well or runs wrong randomly when I activate or deactivate
tf.reset_default_graph()
. I don't know why it happens and please let me know how to solve this issue.import numpy as np import tensorflow as tf import matplotlib.pyplot as plt def shuffle_data(x_train, y_train): temp_index = np.arange(len(x_train)) np.random.shuffle(temp_index) x_temp = np.zeros(x_train.shape) y_temp = np.zeros(y_train.shape) x_temp = x_train[temp_index] y_temp = y_train[temp_index] return x_temp, y_temp def main(): num_points = 2000 vectors_set = [] for i in range(num_points): x1=np.random.normal(0,1.0) y1=np.sin(x1)+np.random.normal(0.,0.1) vectors_set.append([x1,y1]) x_data = [v[0] for v in vectors_set] y_data = [v[1] for v in vectors_set] plt.plot(x_data, y_data, 'go') plt.legend("t") plt.show() BATCH_SIZE = 100 BATCH_NUM = int(len(x_data)/BATCH_SIZE) x_data = np.reshape(x_data, [len(x_data),1]) y_data = np.reshape(y_data, [len(y_data),1]) input_data = tf.placeholder(tf.float32, shape = [None,1]) output_data = tf.placeholder(tf.float32, shape = [None,1]) W1 = tf.Variable(tf.random_uniform([1,5],1.0,1.0),name="w1") W2 = tf.Variable(tf.random_uniform([5,3],1.0,1.0),name="w2") W_out = tf.Variable(tf.random_uniform([3,1],1.0,1.0),name="wo") hidden1=tf.nn.sigmoid(tf.matmul(input_data,W1)) hidden2=tf.nn.sigmoid(tf.matmul(hidden1,W2)) output = tf.matmul(hidden2, W_out, name="output") loss=tf.reduce_mean(tf.square(outputoutput_data)) optimizer = tf.train.AdamOptimizer(0.01) train=optimizer.minimize(loss) init = tf.global_variables_initializer() saver = tf.train.Saver() sess = tf.Session() ckpt=tf.train.get_checkpoint_state("tmp") if(ckpt!=None) and tf.train.checkpoint_exists(ckpt.model_checkpoint_path): print("varaible is restored") tf.reset_default_graph() print(ckpt.model_checkpoint_path) saver.restore(sess, ckpt.model_checkpoint_path) else: print("failed to restor. init graph") sess.run(init) for step in range(num_points+1): index = 0 x_data, y_data = shuffle_data(x_data, y_data) for batch_iter in range(BATCH_NUM1): feed_dict = {input_data: x_data[index:index+BATCH_SIZE], output_data:y_data[index:index+BATCH_SIZE]} sess.run(train, feed_dict = feed_dict) index += BATCH_SIZE if (step%1000==0): print("Step=%5d, Loss Value = %f" %(step, sess.run(loss, feed_dict = feed_dict))) save_path = saver.save(sess, "tmp/train", global_step=step) print("Model saved in file: %s" % save_path) feed_dict = {input_data: x_data} plt.plot(x_data, y_data, 'go') plt.plot(x_data, sess.run(output, feed_dict=feed_dict), 'k*') plt.xlabel('x') plt.xlim(4,3) plt.ylabel('y') plt.ylim(1.5, 1.5) plt.legend("f") plt.show() if __name__ == "__main__": main()

Find and display data within a blob
I have a blob within which I have 3 letters. I have extracted the blob information, I have also used contour analysis to find contours of the letters and use it as OCR to detect it as a character using emguCV. I'm using a pictureBox1_Click event to display the blob info when user click on the blob. But instead of displaying all the 3 letters within the blob, the logic displays the letter on which i click within the blob. If I click on A it shows A, but I want that wherever i click within blob all 3 should be shown in the textbox.
Im checking whether the X and Y coordinate of the letter is lower than the X and Y coordinate of the blob.
Here is my code :
private void pictureBox1_MouseClick(object sender, MouseEventArgs e) { int x = e.X  1; int y = e.Y  1; if ((pictureBox1.Image != null) && (x >= 0) && (y >= 0) && (x < pictureBox1.Image.Width) && (y < pictureBox1.Image.Height)) { int blobID = blobCounter.ObjectLabels[y * pictureBox1.Image.Width + x]; if (blobID != 0) { Invalidate(); string s = ""; foreach (Blob blobss in blobs) { if(blobss.ID == blobID) { //Extract letter coordinates and name foreach (FoundCharacter found in processor.foundTemplates) { Rectangle foundRect = found.sample.contour.SourceBoundingRect; int x1 = foundRect.X; int y1 = foundRect.Y; s = found.template.name; if ((x1 < x) && (y1 < y)) { txtchar.Text = s; } } } } } } }

How to calculate homogeneous coordinates
I am learning about projective geometry and read about homogeneous coordinates and their role in projective geometry. I have 2 images named
A.png
andB.png
and I want to projectB.png
onA.png
.How could I find homogeneous coordinates that are required for projecting image
B
onA
?I already calculated the transformation matrix (homography).

Why is labeling training data this way leading to bad object detection when retraining pretrained models using the tensorflow object detection api?
So I've been messing around with tensorflow's object detection api and specifically the retraining of models, essentially doing this. I made it detect my object fairly well with a small number of images. But I wanted to increase the number of images I train with, however the labeling process is long and boring so I found a data set with cropped images, so only my object is in the image.
If there's a way to send whole images without labeling them too be trained using tensorflow api I didn't find it but I thought making a program that labels the whole image would not be that hard.
The format of the labeling is a csv file with these entries: filename, width, height, class, xmin, ymin, xmax, ymax.
This is my code:
import os import cv2 path = "D:/path/to/image/folder" directory = os.fsencode(path) text = open("D:/result/train.txt","w") for file in os.listdir(directory): filename = os.fsdecode(file) if filename.endswith(".jpg"): impath= path + "/" + filename img = cv2.imread(impath) res = filename+","+ str(img.shape[1])+","+str(img.shape[0])+",person,1,1,"+str(img.shape[1]1) +"," +str(img.shape[0]1)+"\n" text.write(res) print(res) text.close()
This seems to be working fine.
Now here's the problem. After converting the .txt to .csv and running the training until the loss stops decreasing my detection on my test set are awful. It puts a huge bounding box around the entirety of the image like it's trained to detect only the edges of the image.
I figure it's somehow learning to detect the edges of the images since the labeling is around the whole image. But how do I make it learn to "see" what's in the picture? Any help would be appreciated.

Image preprocessing in convolutional neural network yields lower accuracy in Keras vs Tflearn
I'm trying to convert this tflearn DCNN sample (using image preprocessing and augmemtation) to keras:
Tflearn sample:
import tflearn from tflearn.data_utils import shuffle, to_categorical from tflearn.layers.core import input_data, dropout, fully_connected from tflearn.layers.conv import conv_2d, max_pool_2d from tflearn.layers.estimator import regression from tflearn.data_preprocessing import ImagePreprocessing from tflearn.data_augmentation import ImageAugmentation # Data loading and preprocessing from tflearn.datasets import cifar10 (X, Y), (X_test, Y_test) = cifar10.load_data() X, Y = shuffle(X, Y) Y = to_categorical(Y, 10) Y_test = to_categorical(Y_test, 10) # Realtime data preprocessing img_prep = ImagePreprocessing() img_prep.add_featurewise_zero_center() img_prep.add_featurewise_stdnorm() # Realtime data augmentation img_aug = ImageAugmentation() img_aug.add_random_flip_leftright() img_aug.add_random_rotation(max_angle=25.) # Convolutional network building network = input_data(shape=[None, 32, 32, 3], data_preprocessing=img_prep, data_augmentation=img_aug) network = conv_2d(network, 32, 3, activation='relu') network = max_pool_2d(network, 2) network = conv_2d(network, 64, 3, activation='relu') network = conv_2d(network, 64, 3, activation='relu') network = max_pool_2d(network, 2) network = fully_connected(network, 512, activation='relu') network = dropout(network, 0.5) network = fully_connected(network, 10, activation='softmax') network = regression(network, optimizer='adam', loss='categorical_crossentropy', learning_rate=0.001) # Train using classifier model = tflearn.DNN(network, tensorboard_verbose=0) model.fit(X, Y, n_epoch=50, shuffle=True, validation_set=(X_test, Y_test), show_metric=True, batch_size=96, run_id='cifar10_cnn')
This yielded the following results after 50 epochs:
Training Step: 26050  total loss: 0.35260  time: 144.306s  Adam  epoch: 050  loss: 0.35260  acc: 0.8785  val_loss: 0.64622  val_acc: 0.8212  iter: 50000/50000
I then tried to convert it to Keras using the same DCNN layers, parameters and image preprocessing/augmentation:
import numpy as np from keras.datasets import cifar10 from keras.callbacks import TensorBoard from keras.models import Sequential from keras.layers import Dense, Dropout, Activation, Flatten from keras.layers import Conv2D, MaxPooling2D, AveragePooling2D, UpSampling2D, AtrousConvolution2D from keras.layers.advanced_activations import LeakyReLU, PReLU from keras.utils import np_utils from keras.preprocessing.image import ImageDataGenerator from keras import backend as K import matplotlib from matplotlib import pyplot as plt np.random.seed(1337) batch_size = 96 # how many images to process at once nb_classes = 10 # how many types of objects we can detect in this set nb_epoch = 50 # how long we train the system img_rows, img_cols = 32, 32 # image dimensions nb_filters = 32 # number of convolutional filters to use pool_size = (2, 2) # size of pooling area for max pooling kernel_size = (3, 3) # convolution kernel size (X_train, Y_train), (X_test, Y_test) = cifar10.load_data() X_train = X_train.reshape(X_train.shape[0], img_rows, img_cols, 3) X_test = X_test.reshape(X_test.shape[0], img_rows, img_cols, 3) input_shape = (img_rows, img_cols, 3) X_train = X_train.astype('float32') X_test = X_test.astype('float32') X_train /= 255 X_test /= 255 print('X_train shape:', X_train.shape) print(X_train.shape[0], 'train samples') print(X_test.shape[0], 'test samples') # convert class vectors to binary class matrices Y_train = np_utils.to_categorical(Y_train, nb_classes) Y_test = np_utils.to_categorical(Y_test, nb_classes) datagen = ImageDataGenerator(featurewise_center=True, featurewise_std_normalization=True, horizontal_flip=True, rotation_range=25) datagen.fit(X_train) model = Sequential() model.add(Conv2D(nb_filters, kernel_size, padding='valid', input_shape=input_shape, activation='relu')) model.add(MaxPooling2D(pool_size=pool_size)) model.add(Conv2D(nb_filters*2, kernel_size, activation='relu')) model.add(Conv2D(nb_filters*2, kernel_size, activation='relu')) model.add(MaxPooling2D(pool_size=pool_size)) model.add(Flatten()) model.add(Dense(512, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(nb_classes, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) # Set up TensorBoard tb = TensorBoard(log_dir='./logs') history = model.fit_generator(datagen.flow(X_train, Y_train, batch_size=batch_size), epochs=nb_epoch, shuffle=True, verbose=1, validation_data=(X_test, Y_test), callbacks=[tb]) score = model.evaluate(X_test, Y_test, verbose=0) print('Test score:', score[0]) print("Accuracy: %.2f%%" % (score[1]*100)) plt.plot(history.epoch,history.history['val_acc'],'o',label='validation') plt.plot(history.epoch,history.history['acc'],'o',label='training') plt.legend(loc=0) plt.xlabel('epochs') plt.ylabel('accuracy') plt.grid(True) plt.show()
This yielded far worse validation accuracy results:
Epoch 50/50 521/521 [==============================]  84s 162ms/step  loss: 0.4723  acc: 0.8340  val_loss: 3.2970  val_acc: 0.2729 Test score: 3.2969648239135743 Accuracy: 27.29%
Can anyone help me understand why? Have I misapplied/misunderstood image preprocessing/augmentation in Keras?

Reshaping RGB image is not consistent with Lecture Notes(coursera)
I:m taking the coursera deep learning specialization course and got confusion about reshaping RGB image into an array.
image = np.array([[[ 0.67826139, 0.29380381], [ 0.90714982, 0.52835647], [ 0.4215251 , 0.45017551]], [[ 0.92814219, 0.96677647], [ 0.85304703, 0.52351845], [ 0.19981397, 0.27417313]], [[ 0.60659855, 0.00533165], [ 0.10820313, 0.49978937], [ 0.34144279, 0.94630077]]]) print ("image2vector(image) = " + str(image2vector(image)))
Here is an image of shape (3 ,3 ,2) before turning into one dimensional vector. So here 2 is the number of channel of the image which is white and black respectively. The instruction of Assignment uses X_flatten = X.reshape(X.shape[0], 1).T to reshape the image. We get the following array.
Reshaping RGB image is not consistent with Lecture Notes Weiguo WangWeek 2 · 41 minutes ago · Edited
Here is an image of shape (3 ,3 ,2) before turning into one dimensional vector. So here 2 is the number of channel of the image which is white and black respectively. The instruction of Assignment uses X_flatten = X.reshape(X.shape[0], 1).T to reshape the image. We get the following array.
Clearly the procedure turning the original image into one dimensional vector by doing the following operations:
Original image pixel 1 W channel followed by pixel 1 B channel  pixel 2 W channel followed by pixel 2 B channel pixel 3W channel followed by pixel 3 B channel pixel 4 W channel followed by pixel 4 B channel ..................pixel 9 W channel followed by pixel 9 B channel
But the instruction provided is not consistent with instruction on the Lecture notes which flattening the Red channel into an array then Green and Blue, finally concatenating them. This has been confusing me for a while. I would appreciate for your help. Thank you.

Pipeline for image classification with small objects relative to image size
I have a dataset of images, which are mostly background noise except for tiny regions where there is an object of interest (one per image). Each image has a prediction target of either 0 or 1, indicating the class of the object.
My goal is to train a NN which will perform a binary classification on the object of interest.
What kind of pipeline should I use?
Just an endtoend CNN?
Is there some simple way of, I dunno, break the problem into a) find the object of interest b) classify it?
Can I train and predict better by having my model use sliding windows somehow, even if I do not have labels on each patch of the image?
Examples of problems that fall into this scheme: tumor classification (malignant/benignant), distinguishing between planes and birds in the sky near an airport