why Keras 2D regression network has constant output - keras

I am working on the some kind of the 2D Regression Deep network with keras, but the network has constant output for every datasets, even I test with handmade dataset in this code I feed the network with a constant 2d values and the output is linear valu of the X (2*X/100) but the out put is constant.
import resource
import glob
import gc
rsrc = resource.RLIMIT_DATA
soft, hard = resource.getrlimit(rsrc)
print ('Soft limit starts as :', soft)
resource.setrlimit(rsrc, (4 * 1024 * 1024 * 1024, hard)) # limit to four giga bytes
soft, hard = resource.getrlimit(rsrc)
print ('Soft limit changed to :', soft)
from keras.models import Sequential
import keras.optimizers
from keras.layers import Dense, Dropout, Activation, Flatten, BatchNormalization
from keras.layers import Convolution2D, MaxPooling2D,AveragePooling2D
import numpy as np
import random
from keras.utils import plot_model
sample_size = 1
batch_size = 50
input_shape = (int(720 / 4), int(1280 / 4), sample_size * 5)
# model
model = Sequential()
model.add(BatchNormalization(input_shape=input_shape))
model.add(Convolution2D(128, (3, 3), activation='relu', dim_ordering="tf", padding="same",kernel_initializer='random_uniform'))
model.add(Convolution2D(128, (3, 3), activation='sigmoid', dim_ordering="tf", padding="same",kernel_initializer='random_uniform'))
model.add(AveragePooling2D(pool_size=(4, 4), dim_ordering="tf"))
model.add(Convolution2D(256, (3, 3), activation='sigmoid', dim_ordering="tf", padding="same",kernel_initializer='random_uniform'))
model.add(Convolution2D(256, (3, 3), activation='sigmoid', dim_ordering="tf", padding="same",kernel_initializer='random_uniform'))
model.add(AveragePooling2D(pool_size=(4, 4), dim_ordering="tf"))
model.add(Convolution2D(512, (3, 3), activation='sigmoid', dim_ordering="tf", padding="same",kernel_initializer='random_uniform'))
model.add(Convolution2D(512, (3, 3), activation='sigmoid', dim_ordering="tf", padding="same",kernel_initializer='random_uniform'))
model.add(AveragePooling2D(pool_size=(4, 4), dim_ordering="tf"))
model.add(Flatten())
model.add(Dense(4096, activation='relu',kernel_initializer='random_uniform'))
#model.add(Dropout(0.5))
model.add(Dense(512, activation='sigmoid',kernel_initializer='random_uniform'))
model.add(Dense(1, activation='sigmoid',kernel_initializer='random_uniform'))
model.compile(loss='mean_absolute_error',
optimizer='adam',
metrics=['mae','mse'])
model.summary()
plot_model(model,to_file='model.png')
def generate_tr(batch_size, is_training=False):
x=np.linspace(0, 10, num=5000).reshape(-1, 1)
counter = 0
print 'start'
while 1:
samples=np.zeros((batch_size, 720/4, 1280/4, 5))
labels=[]
for t in range (batch_size):
i = int(random.randint(0, 4999))
for b in range(sample_size):
samples[t, :,:,b*5:b*5+5] = np.random.rand(720/4,1280/4,5)/10+x[i]
labels.append((2*x[i])/100)
counter += 1
print counter #, labels
yield ((samples), np.asarray(labels))
tt = model.fit_generator(generate_tr(batch_size, True), steps_per_epoch=100, epochs=10,
use_multiprocessing=False, verbose=2)
score = model.predict_generator(generate_tr(batch_size, True), steps=30)
the output is always average of all of the values (here is .10)
do you know why?

Related

Keras parallel layers merging

I read Keras documentation, but had not found any explanation on the following error
Code:
import numpy as np
import pandas as pd
from tensorflow.keras import layers
from keras.optimizers import SGD
from keras.models import Sequential
from keras.layers.convolutional import Convolution2D, MaxPooling2D, ZeroPadding2D
from keras.layers.core import Dense, Activation, Dropout, Reshape, Flatten
from keras.utils.np_utils import to_categorical
data = pd.read_excel('oildata.xlsx')
firstBranch = Sequential()
#firstBranch.add(Reshape((1,28,28), input_shape=(,)))
firstBranch.add(LSTM(64, input_shape=(10, 1100)))
#firstBranch.add(MaxPooling2D((2, 2), strides=(2, 2)))
firstBranch.add(Flatten())
secondBranch = Sequential()
secondBranch.add(BatchNormalization(name = 'batch_norm_0', input_shape = (1000, 10, 1, 1)))
secondBranch.add(ConvLSTM2D(name ='conv_lstm_1',
filters = 64, kernel_size = (10, 1),
padding = 'same',
return_sequences = False))
secondBranch.add(Dropout(0.10, name = 'dropout_1'))
secondBranch.add(BatchNormalization(name = 'batch_norm_1'))
# model.add(ConvLSTM2D(name ='conv_lstm_2',
# filters = 64, kernel_size = (5, 1),
# padding='same',
# return_sequences = False))
# model.add(Dropout(0.20, name = 'dropout_2'))
# model.add(BatchNormalization(name = 'batch_norm_2'))
secondBranch.add(Flatten())
secondBranch.add(RepeatVector(1000))
secondBranch.add(Reshape((1000, 10, 1, 64)))
# model.add(ConvLSTM2D(name ='conv_lstm_3',
# filters = 64, kernel_size = (10, 1),
# padding='same',
# return_sequences = True))
# model.add(Dropout(0.20, name = 'dropout_3'))
# model.add(BatchNormalization(name = 'batch_norm_3'))
secondBranch.add(ConvLSTM2D(name ='conv_lstm_4',
filters = 64, kernel_size = (5, 1),
padding='same',
return_sequences = True))
secondBranch.add(TimeDistributed(Dense(units=1, name = 'dense_1', activation = 'relu')))
secondBranch.add(Dense(units=1, name = 'dense_2'))
secondBranch.add(Flatten())
thirdBranch = Sequential()
thirdBranch.add(Reshape((1,28,28), input_shape=(784,)))
thirdBranch.add(Dense(10, activation='relu'))
thirdBranch.add(Flatten())
fourthBranch = Sequential()
#fourthBranch.add(Reshape((1,28,28), input_shape=(784,)))
fourthBranch.add(Dense(10, activation='relu'))
fourthBranch.add(Flatten())
#merged = Concatenate([firstBranch, secondBranch, thirdBranch,fourthBranch], mode='concat')
merged = Concatenate([firstBranch,secondBranch,thirdBranch,fourthBranch])
model = Sequential()
model.add(merged)
model.add(Dense(28*3, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(28, activation='relu'))
model.add(Dense(19))
model.add(Activation("softmax"))
sgd = SGD(lr=0.5, momentum=0.8, decay=0.1, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
model.fit([X,X,X,X],X, batch_size=100, verbose=2)
yPred = model.predict([X,X,X,X],X)
Error:
TypeError Traceback (most recent call last)
<ipython-input-385-11a86cc54884> in <module>
88 sgd = SGD(lr=0.5, momentum=0.0, decay=0.0, nesterov=False)
89 model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
---> 90 model.fit([X,X,X,X],X, batch_size=100, verbose=2)
91
92 yPred = model.predict([X,X,X,X],X)
...........................................
TypeError: list indices must be integers or slices, not ListWrapper"
What does it mean ListWrapper? Data is turned into frames and had to fit the model.

How to change the Keras sample code model.fit to generator manner?

I'm trying to implement a CNN+RNN+LSTM structure(1) with Keras.
And I found a related Keras sample code.
How can I convert the model.fit to model.fit_generator correctly?
Original code:
from keras.models import Sequential
from keras.layers import Activation, MaxPooling2D, Dropout, LSTM, Flatten, Merge, TimeDistributed
import numpy as np
from keras.layers import Concatenate
from keras.layers.convolutional import Conv2D
# Generate fake data
# Assumed to be 1730 grayscale video frames
x_data = np.random.random((1730, 1, 8, 10))
sequence_lengths = None
Izda=Sequential()
Izda.add(TimeDistributed(Conv2D(40,(3,3),padding='same'), input_shape=(sequence_lengths, 1,8,10)))
Izda.add(Activation('relu'))
Izda.add(TimeDistributed(MaxPooling2D(data_format="channels_first", pool_size=(2, 2))))
Izda.add(Dropout(0.2))
Dcha=Sequential()
Dcha.add(TimeDistributed(Conv2D(40,(3,3),padding='same'), input_shape=(sequence_lengths, 1,8,10)))
Dcha.add(Activation('relu'))
Dcha.add(TimeDistributed(MaxPooling2D(data_format="channels_first", pool_size=(2, 2))))
Dcha.add(Dropout(0.2))
Frt=Sequential()
Frt.add(TimeDistributed(Conv2D(40,(3,3),padding='same'), input_shape=(sequence_lengths, 1,8,10)))
Frt.add(Activation('relu'))
Frt.add(TimeDistributed(MaxPooling2D(data_format="channels_first", pool_size=(2, 2))))
Frt.add(Dropout(0.2))
merged=Merge([Izda, Dcha,Frt], mode='concat', concat_axis=2)
#merged=Concatenate()([Izda, Dcha, Frt], axis=2)
# Output from merge is (batch_size, sequence_length, 120, 4, 5)
# We want to get this down to (batch_size, sequence_length, 120*4*5)
model=Sequential()
model.add(merged)
model.add(TimeDistributed(Flatten()))
model.add(LSTM(240, return_sequences=True))
model.compile(loss='mse', optimizer='adam')
model.summary()
After my modification:
from keras.models import Sequential
from keras.layers import Activation, MaxPooling2D, Dropout, LSTM, Flatten, Merge, TimeDistributed
import numpy as np
from keras.layers import Concatenate
from keras.layers.convolutional import Conv2D
# Generate fake data
# Assumed to be 1730 grayscale video frames
x_data = np.random.random((1730, 1, 8, 10))
sequence_lengths = None
def defModel():
model=Sequential()
model.add(TimeDistributed(Conv2D(40,(3,3),padding='same'), input_shape=(sequence_lengths, 1,8,10)))
model.add(Activation('relu'))
model.add(TimeDistributed(MaxPooling2D(data_format="channels_first", pool_size=(2, 2))))
model.add(Dropout(0.2))
model.add(TimeDistributed(Flatten()))
model.add(LSTM(240, return_sequences=True))
model.compile(loss='mse', optimizer='adam')
model.summary()
return model
def gen():
for i in range(1730):
x_train = np.random.random((1, 8, 10))
y_train = np.ones((15, 240))
yield (x_train, y_train)
def main():
model = defModel()
# Slice our long, single sequence up into shorter sequeunces of images
# Let's make 50 examples of 15 frame videos
x_train = []
seq_len = 15
for i in range(50):
x_train.append(x_data[i*5:i*5+seq_len, :, :, :])
x_train = np.asarray(x_train, dtype='float32')
print(x_train.shape)
# >> (50, 15, 1, 8, 10)
model.fit_generator(
generator = gen(),
steps_per_epoch = 1,
epochs = 2)
if __name__ == "__main__":
main()
How can I resolve this error produce from by my modification?
ValueError: Error when checking input: expected
time_distributed_1_input to have 5 dimensions, but got array with
shape (1, 8, 10)
(1) Wang, S., Clark, R., Wen, H., & Trigoni, N. (2017). DeepVO: Towards end-to-end visual odometry with deep Recurrent Convolutional Neural Networks. Proceedings - IEEE International Conference on Robotics and Automation, 2043–2050.
Update: Concatenate CNN and LSTM as sample code
model.add(TimeDistributed(Conv2D(16, (7, 7),padding='same'),input_shape=(None, 540, 960, 1)))
model.add(Activation('relu'))
model.add(TimeDistributed(Conv2D(32, (5, 5),padding='same'))) model.add(Activation('relu'))
model.add(TimeDistributed(Flatten()))
model.add(LSTM(num_classes, return_sequences=True))
Got error
ValueError: Error when checking target: expected lstm_1 to have 3 dimensions, but got array with shape (4, 3)
Update2
The goal is to extract image feature by CNN, then combine 3 feature from 3 images and feed into LSTM.
Goal
#Input image
(540, 960, 1) ==> (x,y,ch) ==> CNN ==> (m,n,k)┐
(540, 960, 1) ==> (x,y,ch) ==> CNN ==> (m,n,k)---> (3, m,n,k) --flatten--> (3, mnk)
(540, 960, 1) ==> (x,y,ch) ==> CNN ==> (m,n,k)」
(3, mnk) => LSTM => predict three regression value
Model
model = Sequential()
model.add(TimeDistributed(Conv2D(16, (7, 7), padding='same'),input_shape=(None, 540, 960, 1)))
model.add(Activation('relu'))
model.add(TimeDistributed(Conv2D(32, (5, 5), padding='same')))
model.add(Activation('relu'))
model.add(TimeDistributed(Flatten()))
model.add(LSTM(num_classes, return_sequences=True))
model.compile(loss='mean_squared_error', optimizer='adam')
The generator
a = readIMG(filenames[start]) # (540, 960, 1)
b = readIMG(filenames[start + 1]) # (540, 960, 1)
c = readIMG(filenames[start + 2]) # (540, 960, 1)
x_train = np.array([[a, b, c]]) # (1, 3, 540, 960, 1)
Then I still got the error:
ValueError: Error when checking target: expected lstm_1 to have 3 dimensions, but got array with shape (1, 3)
The problem is a plain shape mismatch problem.
You defined input_shape=(sequence_lengths, 1,8,10), so your model is expecting five dimensions as input: (batch_size, sequence_lengths, 1, 8, 10)
All you need is to make your generator output the correct shapes with 5 dimensions.
def gen():
x_data = np.random.random((numberOfVideos, videoLength, 1, 8, 10))
y_data = np.ones((numberOfVideos, videoLength, 240))
for video in range(numberOfVideos):
x_train = x_data[video:video+1]
y_train = y_data[video:video+1]
yield (x_train, y_train)
Here is the working example of CNNLSTM using generator: https://gist.github.com/HTLife/25c0cd362faa91477b8f28f6033adb45

How to extract feature vector for image when using CNN in Keras

I am doing a binary classification problem, my model architecture is as follow
def CNN_model(height, width, depth):
input_shape = (height, width, depth)
model = Sequential()
# Block 1
model.add(Conv2D(64, kernel_size=(3, 3), strides=1, activation='relu', input_shape=input_shape, padding='VALID'))
model.add(Conv2D(64, kernel_size=(3, 3), strides=1, activation='relu', padding='VALID'))
model.add(MaxPooling2D(pool_size=(2, 2)))
# Block 2
model.add(Conv2D(128, kernel_size=(3, 3), strides=1, activation='relu', padding='VALID'))
model.add(Conv2D(128, kernel_size=(3, 3), strides=1, activation='relu', padding='VALID'))
model.add(AveragePooling2D(pool_size=(19, 19)))
# set of FC => RELU layers
model.add(Flatten())
model.add(Dense(128))
model.add(Activation('relu'))
model.add(BatchNormalization())
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss=keras.losses.binary_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
return model
I need for each image on a test set, I get a 128-D feature vector collected from FC layer use for SVM classification. More detail, from model.add(Dense(128)). Can you please show me how to solve this problem? Thank you!
Here the simplest way is to remove the Dense layer.
I will answer with a counter example with similar layers but different input_shape:
from keras.layers import *
from keras.preprocessing import image
from keras.applications.vgg16 import VGG16
from keras.applications.vgg16 import preprocess_input
import numpy as np
from scipy.misc import imsave
import numpy as np
from keras.layers import *
from keras.applications.vgg16 import VGG16
from keras.applications.vgg16 import preprocess_input
from keras.layers import Dropout, Flatten, Dense
from keras.applications import ResNet50
from keras.models import Model, Sequential
from keras.layers import Dense, GlobalAveragePooling2D
from keras import backend as K
import matplotlib.pyplot as plt
from keras.applications.resnet50 import preprocess_input
model = Sequential()
model.add(Conv2D(64, kernel_size=(3, 3), input_shape=(530, 700, 3), padding='VALID'))
model.add(Conv2D(64, kernel_size=(3, 3), padding='VALID'))
model.add(MaxPooling2D(pool_size=(2, 2)))
# Block 2
model.add(Conv2D(128, kernel_size=(3, 3), strides=1, activation='relu', padding='VALID'))
model.add(Conv2D(128, kernel_size=(3, 3), strides=1, activation='relu', padding='VALID'))
model.add(AveragePooling2D(pool_size=(19, 19)))
# set of FC => RELU layers
model.add(Flatten())
#getting the summary of the model (architecture)
model.summary()
img_path = '/home/sb0709/Desktop/dqn/DQN/data/data/2016_11_01-2017_11_01.png'
img = image.load_img(img_path, target_size=(530, 700))
img_data = image.img_to_array(img)
img_data = np.expand_dims(img_data, axis=0)
img_data = preprocess_input(img_data)
vgg_feature = model.predict(img_data)
#print the shape of the output (so from your architecture is clear will be (1, 128))
#print shape
print(vgg_feature.shape)
#print the numpy array output flatten layer
print(vgg_feature.shape)
Here is the output model architecture with all layers:
Also here is listed the feature vector:
Image used in the example:
Second method is for when using Functional Api instead of Sequencial() to use How can I obtain the output of an intermediate layer?
from keras import backend as K
# with a Sequential model
get_6rd_layer_output = K.function([model.layers[0].input],
[model.layers[6].output])
layer_output = get_6rd_layer_output([x])[0]
#print shape
print(layer_output.shape)
#print the numpy array output flatten layer
print(layer_output.shape)
One more useful step is the visualization of the features, I bet a lot of people want to see what see the computer and will illustrate only the "Flatten" layer output(better said the network):
def visualize_stock(img_data):
plt.figure(1, figsize=(25, 25))
stock = np.squeeze(img_data, axis=0)
print(stock.shape)
plt.imshow(stock)
and the magic:
visualize_stock(img_data)
Note: changed from input_shape=(530, 700, 3) from input_shape=(84, 84, 3) for better visualization for the public.
P.S: Decided to post so anyone who has this type of question to benefit (struggled with same type of questions recently).

Auto-encoder Mismatch Dimension Error

I am trying to reconstruct images using Conv Autoencoder, but I get an error related to dimensions, could you find a solution, thanks,
Basically fist I want to test the model on reconstructing the same input data which are images, then if the model worked fine, I need to model images to maps,
In this case, can I just change the data from image_data to map_data as shown in the code below:
from keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D
from keras.models import Model
from keras import backend as K
from keras.callbacks import TensorBoard
import numpy as np
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.utils import np_utils
import matplotlib.pyplot as plt
import matplotlib
import os
from PIL import Image
from numpy import *
from sklearn.utils import shuffle
from sklearn.cross_validation import train_test_split
from keras.preprocessing.image import ImageDataGenerator,array_to_img, img_to_array, load_img
image_data='C:/Users/user_PC/Desktop/Image2Map/Samples'
map_data='C:/Users/user_PC/Desktop/Image2Map/Samples'
K.set_image_dim_ordering('tf')
input_img = Input(batch_shape=(1024, 106, 106,3))
x = Conv2D(16, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
x = Conv2D(16, (3, 3), activation='relu')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)
autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
train_datagen = ImageDataGenerator(
rescale=1. / 255)
test_datagen = ImageDataGenerator(rescale=1. / 255)
train_generator = train_datagen.flow_from_directory(
image_data,
target_size=(106, 106),
batch_size=4,
class_mode=None)
validation_generator = test_datagen.flow_from_directory(
image_data,
target_size=(106, 106),
batch_size=4,
class_mode=None)
imgs = np.concatenate([train_generator.next()[0] for i in range(1024)])
autoencoder.fit_generator(generator=(imgs,imgs),
samples_per_epoch=1024 // 4,
epochs=10,
validation_data=(imgs,imgs),
validation_steps=1024 // 4)
decoded_imgs = autoencoder.predict(image_data)
n = 10
plt.figure(figsize=(20, 4))
for i in range(n):
# display original
ax = plt.subplot(2, n, i)
plt.imshow(image_data[i].reshape(106, 106))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
# display reconstruction
ax = plt.subplot(2, n, i + n)
plt.imshow(decoded_imgs[i].reshape(106, 106))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.show()
Change class_mode from 'None' to 'input'. It should work . read more here

Model input error

Here is the error in full:
Exception: Error when checking model input: expected convolution2d_input_1 to have shape (None, 3, 224, 224) but got array with shape (20, 3, 244, 244)
Everything works until the final model.fit_generator(...) chunk of code. I am using a theano backend.
I'm pretty new to keras, so I'm not sure exactly how to proceed. Checking the documentation I can see that the None in layers.convolutional.Convolution2D corresponds to the number of batches (or samples)? Substituting input_shape=(20,3,244,244) yielded the following error Exception: Input 0 is incompatible with layer conv1_1: expected ndim=4, found ndim=5. Using 23000 instead of 20 yielded the same error.
Any help is appreciated.
Below is my code:
# ======================
# load data
# ======================
# Set relevant paths for dir structure
current_dir = "/home/ubuntu/nbs/"
DATA_HOME_DIR = current_dir + 'lesson1/data/redux'
path = DATA_HOME_DIR + '/'
train_path = DATA_HOME_DIR + '/train/'
valid_path = DATA_HOME_DIR + '/valid/'
test_path = DATA_HOME_DIR + '/test/'
nb_train_samples = 23000
nb_validation_samples = 2000
nb_epoch = 4
# ======================
# import stuff
# ======================
import numpy as np
from keras.utils.data_utils import get_file
from keras import backend as K
from keras.layers.normalization import BatchNormalization
from keras.models import Sequential
from keras.layers.core import Flatten, Dense, Dropout, Lambda
from keras.layers.convolutional import Convolution2D, MaxPooling2D, ZeroPadding2D
from keras.layers.pooling import GlobalAveragePooling2D
from keras.optimizers import SGD, RMSprop, Adam
from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator
# ======================
# define model
# ======================
def vgg():
model = Sequential()
model.add(Convolution2D(64, 3, 3,input_shape=(3,224,224), activation='relu', name='conv1_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(64, 3, 3, activation='relu', name='conv1_2'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(128, 3, 3, activation='relu', name='conv2_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(128, 3, 3, activation='relu', name='conv2_2'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_2'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_3'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_2'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_3'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_2'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_3'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(Flatten())
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1000, activation='softmax'))
return model
model = vgg()
print model.summary()
#### load weights
fname = 'vgg16.h5'
model.load_weights(get_file(fname, 'http://www.platform.ai/models/'+fname, cache_subdir='models'))
print "successfully created model and loaded weights"
#### Finetune model
model.pop()
for layer in model.layers: layer.trainable=False
model.add(Dense(batches.nb_class, activation='softmax'))
#### Compile model
model.compile(optimizer=Adam(lr=0.01),
loss='categorical_crossentropy', metrics=['accuracy'])
train_datagen = ImageDataGenerator(
rescale = 1./255,
shear_range = 0.2,
zoom_range = 0.2,
horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
train_path,
target_size=(244,244),
batch_size = 20,
class_mode='categorical')
validation_generator = test_datagen.flow_from_directory(
valid_path,
target_size=(244,244),
batch_size=20,
class_mode='categorical')
model.fit_generator(
train_generator,
samples_per_epoch=nb_train_samples,
nb_epoch=nb_epoch,
validation_data=validation_generator,
nb_val_samples=nb_validation_samples)
There is a mismatch between the expected size of the images and the actual one. Your model expects images of size 224 x 224 and according to the attached error message actual size is 244 x 244.

Resources