Skip to content

Instantly share code, notes, and snippets.

Created June 6, 2017 19:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/d283494aee982fbc30f3b52f2a6f422c to your computer and use it in GitHub Desktop.
Save anonymous/d283494aee982fbc30f3b52f2a6f422c to your computer and use it in GitHub Desktop.
I am attempting to build a simple CNN that takes four 100x100 grayscale images as inputs and tells me which one contains a circle. I am having trouble with the interaction between my generator and my CNN inputs, my inputs expect an array of dimension 4 but get an array of shape (100, 100, 1) as expected.
import numpy as np
import os
from keras.models import Model
from keras.layers import Input
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense
from keras.layers.merge import concatenate
input_1 = Input(shape = (100, 100, 1), dtype = 'float32')
input_2 = Input(shape = (100, 100, 1), dtype = 'float32')
input_3 = Input(shape = (100, 100, 1), dtype = 'float32')
input_4 = Input(shape = (100, 100, 1), dtype = 'float32')
output_1 = Conv2D(32, (3, 3), activation = 'relu')(input_1)
output_1 = MaxPooling2D(pool_size = (2, 2))(output_1)
output_1 = Conv2D(32, (3, 3), activation = 'relu')(output_1)
output_1 = MaxPooling2D(pool_size = (2, 2))(output_1)
output_1 = Flatten()(output_1)
output_2 = Conv2D(32, (3, 3), activation = 'relu')(input_2)
output_2 = MaxPooling2D(pool_size = (2, 2))(output_2)
output_2 = Conv2D(32, (3, 3), activation = 'relu')(output_2)
output_2 = MaxPooling2D(pool_size = (2, 2))(output_2)
output_2 = Flatten()(output_2)
output_3 = Conv2D(32, (3, 3), activation = 'relu')(input_3)
output_3 = MaxPooling2D(pool_size = (2, 2))(output_3)
output_3 = Conv2D(32, (3, 3), activation = 'relu')(output_3)
output_3 = MaxPooling2D(pool_size = (2, 2))(output_3)
output_3 = Flatten()(output_3)
output_4 = Conv2D(32, (3, 3), activation = 'relu')(input_4)
output_4 = MaxPooling2D(pool_size = (2, 2))(output_4)
output_4 = Conv2D(32, (3, 3), activation = 'relu')(output_4)
output_4 = MaxPooling2D(pool_size = (2, 2))(output_4)
output_4 = Flatten()(output_4)
inputs = [input_1, input_2, input_3, input_4]
outputs = [output_1, output_2, output_3, output_4]
combine = concatenate(outputs)
output = Dense(64, activation = 'relu')(combine)
output = Dense(4, activation = 'sigmoid')(output)
model = Model(inputs = inputs, outputs = [output])
model.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])
from keras.preprocessing.image import ImageDataGenerator, img_to_array, load_img
def input_generator(folder, directories):
Streams = []
for i in range(len(directories)):
Streams.append(os.listdir(folder + "/" + directories[i]))
for j in range(len(Streams[i])):
Streams[i][j] = "Stream" + str(i + 1) + "/" + Streams[i][j]
Streams[i].sort()
length = len(Streams[0])
index = 0
while True:
X = []
y = np.zeros(4)
for Stream in Streams:
image = load_img(folder + '/' + Stream[index], grayscale = True)
array = img_to_array(image)
X.append(array)
y[int(Stream[index][15]) - 1] = 1
index += 1
index = index % length
yield X, y
# Number of epochs
nb_epoch = 100
model.fit_generator(generator = input_generator("multi_shape_database", ['Stream1', 'Stream2', 'Stream3', 'Stream4']),
steps_per_epoch = 12000 / 32,
epochs = nb_epoch)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment