我正在尝试翻译image2image并 我的数据集由Mnist(256 * 256)和变换后的Mnist(256 * 256)组成
我确实遭受了这个错误
ValueError:检查目标时出错:预期conv2d_transpose_57的形状为(256,256,1),但数组的形状为(256,256,3)
我的图层看起来像这样:
conv2d_56(Conv2D)(无,256、256、17)476
conv2d_57(Conv2D)(无,256,256,32)4928
max_pooling2d_37(MaxPooling(None,128,128,32)0
conv2d_58(Conv2D)(无,128、128、48)13872
conv2d_59(Conv2D)(无,128,128,64)27712
max_pooling2d_38(MaxPooling(None,64,64,64)0
max_pooling2d_39(MaxPooling(None,32,32,64)0
conv2d_60(Conv2D)(无,32,32,96)55392
conv2d_61(Conv2D)(无,32,32,128)110720
max_pooling2d_40(MaxPooling(None,16,16,128)0
max_pooling2d_41(MaxPooling(None,8,8,128)0
conv2d_62(Conv2D)(无,8、8、192)221376
conv2d_63(Conv2D)(无,8、8、256)442624
max_pooling2d_42(MaxPooling(None,4,4,4,256)0
conv2d_64(Conv2D)(无,4,4,4,256)590080
conv2d_transpose_50(Conv2DT(None,8,8,256)262400
conv2d_transpose_51(Conv2DT(无人,16、16、192)196800
conv2d_transpose_52(Conv2DT(None,32,32,128)98432
conv2d_transpose_53(Conv2DT(None,64,64,64)32832
conv2d_transpose_54(Conv2DT(None,128,128,48)12336
conv2d_transpose_55(Conv2DT(None,256,256,32)6176
conv2d_transpose_56(Conv2DT(None,256,256,17)561
总参数:2,076,771 可训练的参数:2,076,771 不可训练的参数:0
回溯(最近通话最近一次):
idk为什么会发生
我已将最后一层的输出更改为(None,256,256,1) 我已经将第一层的输入和最后一层的输出更改为(None,256,256,1) 但这不起作用..
import numpy as np
import warnings
import csv
import os
from PIL import Image
import random
from keras.layers import Input
from keras import layers
from keras.layers import Dense
from keras.layers import Activation
from keras.layers import Flatten, Conv2DTranspose
from keras.layers import Conv2D, UpSampling2D
from keras.layers import MaxPooling2D, pooling
from keras.layers import GlobalMaxPooling2D
from keras.layers import ZeroPadding2D
from keras.layers import AveragePooling2D
from keras.layers import GlobalAveragePooling2D
from keras.layers import BatchNormalization
from keras.models import Model
from keras.preprocessing import image
import keras.backend as K
from keras.utils import layer_utils, np_utils
from keras.utils.data_utils import get_file
from keras.applications.imagenet_utils import decode_predictions
from keras.applications.imagenet_utils import preprocess_input
from keras_applications.imagenet_utils import _obtain_input_shape
from keras.engine.topology import get_source_inputs
from keras.models import Sequential
if __name__ == '__main__':
training_file_input = 'C:/Users/my/Desktop/input/train/trainig_tfmed'
training_file_output = 'C:/Users/my/Desktop/input/train/trainig_original'
test_file_input = 'C:/Users/my/Desktop/input/test/test_tfmed'
test_file_output = 'C:/Users/my/Desktop/input/test/test_original'
x_train = []
y_train = []
nop = np.array([None])
training_file_input_list = os.listdir(training_file_input)
test_file_input_list = os.listdir(test_file_input)
for i in range(1, len(training_file_input_list)+1):
input_filename = training_file_input + '/tfmed_trainig_' + str(i) + '.jpg'
input_image = Image.open(input_filename)
input_image = input_image.convert("RGB")
input_image = input_image.resize((256, 256), Image.ANTIALIAS)
input_image = np.array(input_image, dtype=np.float32)
x_train.append(input_image)
#x_train = np.append(nop, input_image)
output_filename = training_file_output + '/original_trainig_' + str(i) + '.jpg'
output_image = Image.open(output_filename)
output_image = output_image.convert("RGB")
output_image = output_image.resize((256, 256), Image.ANTIALIAS)
output_image = np.array(output_image, dtype=np.float32)
y_train.append(output_image)
#y_train = np.append(nop, input_image)
## loading test files
x_test = []
y_test = []
for i in range(1, len(test_file_input_list)+1):
input_filename = test_file_input + '/tfmed_test_' + str(i) + '.jpg'
input_image = Image.open(input_filename)
input_image = input_image.convert("RGB")
input_image = input_image.resize((256, 256), Image.ANTIALIAS)
input_image = np.array(input_image, dtype=np.float32)
x_test.append(input_image)
#x_test = np.append(nop, input_image)
output_filename = test_file_output + '/original_test_' + str(i) + '.jpg'
output_image = Image.open(output_filename)
output_image = output_image.convert("RGB")
output_image = output_image.resize((256, 256), Image.ANTIALIAS)
output_image = np.array(output_image, dtype=np.float32)
y_test.append(output_image)
#y_test = np.append(nop, input_image)
#
x_train = np.asarray(x_train)
y_train = np.asarray(y_train)
x_test = np.asarray(x_test)
y_test = np.asarray(y_test)
print('Model load')
model=Sequential()
model.add(Conv2D(17, (3, 3), padding='same', strides=(1, 1), activation='relu', input_shape = (256, 256, 1)))
print(model.output_shape)
model.add(Conv2D(32, (3, 3), padding='same', strides=(1, 1), activation='relu'))
print(model.output_shape)
model.add(pooling.MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
print(model.output_shape)
model.add(Conv2D(48, (3, 3), padding='same', strides=(1, 1), activation='relu'))
print(model.output_shape)
model.add(Conv2D(64, (3, 3), padding='same', strides=(1, 1), activation='relu'))
print(model.output_shape)
model.add(pooling.MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
print(model.output_shape)
model.add(pooling.MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
print(model.output_shape)
model.add(Conv2D(96, (3, 3), padding='same', strides=(1, 1), activation='relu'))
print(model.output_shape)
model.add(Conv2D(128, (3, 3), padding='same', strides=(1, 1), activation='relu'))
print(model.output_shape)
model.add(pooling.MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
print(model.output_shape)
model.add(pooling.MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
print(model.output_shape)
model.add(Conv2D(192, (3, 3), padding='same', strides=(1, 1), activation='relu'))
print(model.output_shape)
model.add(Conv2D(256, (3, 3), padding='same', strides=(1, 1), activation='relu'))
print(model.output_shape)
model.add(pooling.MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
print(model.output_shape)
model.add(Conv2D(256, (3, 3), padding='same', strides=(1, 1), activation='relu'))
print(model.output_shape)
model.add(Conv2DTranspose(256, kernel_size=(2, 2), strides=(2, 2), activation='relu'))
print(model.output_shape)
model.add(Conv2DTranspose(192, kernel_size=(2, 2), strides=(2, 2), activation='relu'))
print(model.output_shape)
model.add(Conv2DTranspose(128, kernel_size=(2, 2), strides=(2, 2), activation='relu'))
print(model.output_shape)
model.add(Conv2DTranspose(64, kernel_size=(2, 2), strides=(2, 2), activation='relu'))
print(model.output_shape)
model.add(Conv2DTranspose(48, kernel_size=(2, 2), strides=(2, 2), activation='relu'))
print(model.output_shape)
model.add(Conv2DTranspose(32, kernel_size=(2, 2), strides=(2, 2), activation='relu'))
print(model.output_shape)
model.add(Conv2DTranspose(17, kernel_size=(1, 1), strides=(1, 1), activation='relu'))
print(model.output_shape)
model.add(Conv2DTranspose(1, kernel_size=(1, 1), strides=(1, 1), activation='relu'))
print(model.output_shape)
# model.add(Conv2DTranspose(1, kernel_size=(1, 1), strides=(1, 1), activation='relu'))
# print(model.output_shape)
# model.add(UpSampling2D(1))
# print(model.output_shape)
#
model.summary()
model.compile(loss='sparse_categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])
model.fit(x_train, y_train, epochs=10, batch_size=50)
loss, accuracy = model.evaluate(x_test, y_test)
print('Loss: ' + str(loss))
print('Accuracy: ' + str(accuracy))
它应该起作用并且需要学习
答案 0 :(得分:0)
MNIST数据是灰度的,这意味着它的形状为(h,w,1),您已将PIL设置为将数据转换为具有三个颜色尺寸(h,w,3)的RGB,问题是这行:
input_image = input_image.convert("RGB")
将其完全删除或替换为
input_image = input_image.convert("L")
这应该给您输入数据维度(256,256,1)