Reputation: 2743
Significant Edit
Ok so I did a whole rework of this previous questions but still the same issue but now the code is much more concise and easy to read. What I am doing is reading in images from a file using keras.preprocessing image lib. And then convert that into an array using keras img_to_arrar function. Which I parse out into three arrays of anchor array image array and label array. I then pump this through my model which gives me an odd feed back:
Error when checking target: expected Act_3 to have shape (2,) but got array with shape (1,)
Why is it going down to from shape 2 to shape 1 it looks like it loses all of the data.
Here is the full code:
def read_in_images(array):
input_1_array = []
input_2_array = []
labels = []
for item in array:
a = item[0]
i = item[1]
l = item[2]
img_a = image.load_img(a, target_size=(224, 224))
img_i = image.load_img(i, target_size=(224, 224))
a_a = image.img_to_array(img_a)
i_a = image.img_to_array(img_i)
input_1_array.append(a_a)
input_2_array.append(i_a)
labels.append(l)
return np.array(input_1_array), np.array(input_2_array), np.array(labels)
train_x1, train_x2, train_y = read_in_images(sm_train)
val_x1, val_x2, val_y = read_in_images(sm_val)
test_x1, test_x2, test_y = read_in_images(sm_test)
print(train_x1.shape) # give (50, 224, 224, 3)
print(val_x1.shape) # gives (15, 224, 224, 3)
print(test_x1.shape) # (30, 224, 224, 3) which is what I want
resnet_model = resnet50.ResNet50(weights="imagenet", include_top=True)
input_1 = Input(shape=(224,224,3))
input_2 = Input(shape=(224,224,3))
proccess_1 = resnet_model(input_1)
proccess_2 = resnet_model(input_2)
merged = Concatenate(axis=-1)([proccess_1, proccess_2])
fc1 = Dense(512, kernel_initializer="glorot_uniform", name="Den_1")(merged)
fc1 = Dropout(0.2)(fc1)
fc1 = Activation("relu", name = "Act_1")(fc1)
fc2 = Dense(128, kernel_initializer="glorot_uniform", name="Den_2")(fc1)
fc2 = Dropout(0.2)(fc2)
fc2 = Activation("relu", name = "Act_2")(fc2)
pred = Dense(2, kernel_initializer="glorot_uniform", name="Den_3")(fc2)
pred = Activation("softmax", name = "Act_3")(pred)
model = Model(inputs=[input_1, input_2], outputs=pred)
model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
history = model.fit([train_x1, train_x2], train_y,
batch_size=32,
epochs=10,
verbose = 1,
validation_data=([val_x1, val_x2], val_y))
Upvotes: 0
Views: 178
Reputation: 2743
I figured out what my issue was on this new version. I did not make the label in a [0,1] format as it was a 0 or a 1. This will not work with categorical_crossentropy as it needs a [0,1] format for the label. Forgot my basic cat dog classifier.
Upvotes: 2