Error when checking target: expected to have shape (256, 256, 1) but got array with shape (256, 256, 3)
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
i'm trying to make image2image translation and
my dataset is composed of Mnist(256*256) and transformed Mnist(256*256)
I am literally suffering from this error
ValueError: Error when checking target: expected conv2d_transpose_57 to have shape (256, 256, 1) but got array with shape (256, 256, 3)
my layer seems like this :
Layer (type) Output Shape Param #
conv2d_56 (Conv2D) (None, 256, 256, 17) 476
conv2d_57 (Conv2D) (None, 256, 256, 32) 4928
max_pooling2d_37 (MaxPooling (None, 128, 128, 32) 0
conv2d_58 (Conv2D) (None, 128, 128, 48) 13872
conv2d_59 (Conv2D) (None, 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) (None, 32, 32, 96) 55392
conv2d_61 (Conv2D) (None, 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) (None, 8, 8, 192) 221376
conv2d_63 (Conv2D) (None, 8, 8, 256) 442624
max_pooling2d_42 (MaxPooling (None, 4, 4, 256) 0
conv2d_64 (Conv2D) (None, 4, 4, 256) 590080
conv2d_transpose_50 (Conv2DT (None, 8, 8, 256) 262400
conv2d_transpose_51 (Conv2DT (None, 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
conv2d_transpose_57 (Conv2DT (None, 256, 256, 3) 54
Total params: 2,076,771
Trainable params: 2,076,771
Non-trainable params: 0
Traceback (most recent call last):
idk why this is happening
i had changed last layer's output to (None, 256, 256, 1)
i had changed first layer's input and last layer's output to (None, 256, 256, 1)
but it doesn't work..
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))
it should work and it needs to be learned
tensorflow keras
add a comment |
i'm trying to make image2image translation and
my dataset is composed of Mnist(256*256) and transformed Mnist(256*256)
I am literally suffering from this error
ValueError: Error when checking target: expected conv2d_transpose_57 to have shape (256, 256, 1) but got array with shape (256, 256, 3)
my layer seems like this :
Layer (type) Output Shape Param #
conv2d_56 (Conv2D) (None, 256, 256, 17) 476
conv2d_57 (Conv2D) (None, 256, 256, 32) 4928
max_pooling2d_37 (MaxPooling (None, 128, 128, 32) 0
conv2d_58 (Conv2D) (None, 128, 128, 48) 13872
conv2d_59 (Conv2D) (None, 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) (None, 32, 32, 96) 55392
conv2d_61 (Conv2D) (None, 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) (None, 8, 8, 192) 221376
conv2d_63 (Conv2D) (None, 8, 8, 256) 442624
max_pooling2d_42 (MaxPooling (None, 4, 4, 256) 0
conv2d_64 (Conv2D) (None, 4, 4, 256) 590080
conv2d_transpose_50 (Conv2DT (None, 8, 8, 256) 262400
conv2d_transpose_51 (Conv2DT (None, 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
conv2d_transpose_57 (Conv2DT (None, 256, 256, 3) 54
Total params: 2,076,771
Trainable params: 2,076,771
Non-trainable params: 0
Traceback (most recent call last):
idk why this is happening
i had changed last layer's output to (None, 256, 256, 1)
i had changed first layer's input and last layer's output to (None, 256, 256, 1)
but it doesn't work..
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))
it should work and it needs to be learned
tensorflow keras
add a comment |
i'm trying to make image2image translation and
my dataset is composed of Mnist(256*256) and transformed Mnist(256*256)
I am literally suffering from this error
ValueError: Error when checking target: expected conv2d_transpose_57 to have shape (256, 256, 1) but got array with shape (256, 256, 3)
my layer seems like this :
Layer (type) Output Shape Param #
conv2d_56 (Conv2D) (None, 256, 256, 17) 476
conv2d_57 (Conv2D) (None, 256, 256, 32) 4928
max_pooling2d_37 (MaxPooling (None, 128, 128, 32) 0
conv2d_58 (Conv2D) (None, 128, 128, 48) 13872
conv2d_59 (Conv2D) (None, 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) (None, 32, 32, 96) 55392
conv2d_61 (Conv2D) (None, 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) (None, 8, 8, 192) 221376
conv2d_63 (Conv2D) (None, 8, 8, 256) 442624
max_pooling2d_42 (MaxPooling (None, 4, 4, 256) 0
conv2d_64 (Conv2D) (None, 4, 4, 256) 590080
conv2d_transpose_50 (Conv2DT (None, 8, 8, 256) 262400
conv2d_transpose_51 (Conv2DT (None, 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
conv2d_transpose_57 (Conv2DT (None, 256, 256, 3) 54
Total params: 2,076,771
Trainable params: 2,076,771
Non-trainable params: 0
Traceback (most recent call last):
idk why this is happening
i had changed last layer's output to (None, 256, 256, 1)
i had changed first layer's input and last layer's output to (None, 256, 256, 1)
but it doesn't work..
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))
it should work and it needs to be learned
tensorflow keras
i'm trying to make image2image translation and
my dataset is composed of Mnist(256*256) and transformed Mnist(256*256)
I am literally suffering from this error
ValueError: Error when checking target: expected conv2d_transpose_57 to have shape (256, 256, 1) but got array with shape (256, 256, 3)
my layer seems like this :
Layer (type) Output Shape Param #
conv2d_56 (Conv2D) (None, 256, 256, 17) 476
conv2d_57 (Conv2D) (None, 256, 256, 32) 4928
max_pooling2d_37 (MaxPooling (None, 128, 128, 32) 0
conv2d_58 (Conv2D) (None, 128, 128, 48) 13872
conv2d_59 (Conv2D) (None, 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) (None, 32, 32, 96) 55392
conv2d_61 (Conv2D) (None, 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) (None, 8, 8, 192) 221376
conv2d_63 (Conv2D) (None, 8, 8, 256) 442624
max_pooling2d_42 (MaxPooling (None, 4, 4, 256) 0
conv2d_64 (Conv2D) (None, 4, 4, 256) 590080
conv2d_transpose_50 (Conv2DT (None, 8, 8, 256) 262400
conv2d_transpose_51 (Conv2DT (None, 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
conv2d_transpose_57 (Conv2DT (None, 256, 256, 3) 54
Total params: 2,076,771
Trainable params: 2,076,771
Non-trainable params: 0
Traceback (most recent call last):
idk why this is happening
i had changed last layer's output to (None, 256, 256, 1)
i had changed first layer's input and last layer's output to (None, 256, 256, 1)
but it doesn't work..
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))
it should work and it needs to be learned
tensorflow keras
tensorflow keras
asked Jan 4 at 2:39
Jay ParkJay Park
63
63
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
MNIST data is gray scale, which means it has a shape of (h,w,1) you have set PIL to convert the data to RGB which has three colour dimensions (h,w,3), the problem is with this line :
input_image = input_image.convert("RGB")
Either remove it entirely or replace it with
input_image = input_image.convert("L")
This should give your input data dimension (256,256,1)
input_image = Image.open(input_filename) input_image = np.array(input_image, dtype=np.float32) input_image = np.reshape(input_image, (256, 256, 1)) x_test.append(input_image) i've done by this way! it worked! is it fine?
– Jay Park
Jan 7 at 6:40
are you still getting the value error ? or is the model now running ? Reshaping should be ok on this dataset, I am not aware of any full color MNIST datasets so using grayscale or reshaping to (256, 256, 1) are equivalent. You can check this directly by loading up input_image and displaying it as a smoke test
– NiallJG
Jan 7 at 6:50
no it works now! and it seemed okay as i displayed it.
– Jay Park
Jan 8 at 5:52
great, feel free to accept my answer ;)
– NiallJG
Jan 8 at 5:53
but actually i got one more problem, i saved my model and predicted. but result is all black(all zeros). do you know why this is occured?
– Jay Park
Jan 8 at 5:53
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54032486%2ferror-when-checking-target-expected-to-have-shape-256-256-1-but-got-array-w%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
MNIST data is gray scale, which means it has a shape of (h,w,1) you have set PIL to convert the data to RGB which has three colour dimensions (h,w,3), the problem is with this line :
input_image = input_image.convert("RGB")
Either remove it entirely or replace it with
input_image = input_image.convert("L")
This should give your input data dimension (256,256,1)
input_image = Image.open(input_filename) input_image = np.array(input_image, dtype=np.float32) input_image = np.reshape(input_image, (256, 256, 1)) x_test.append(input_image) i've done by this way! it worked! is it fine?
– Jay Park
Jan 7 at 6:40
are you still getting the value error ? or is the model now running ? Reshaping should be ok on this dataset, I am not aware of any full color MNIST datasets so using grayscale or reshaping to (256, 256, 1) are equivalent. You can check this directly by loading up input_image and displaying it as a smoke test
– NiallJG
Jan 7 at 6:50
no it works now! and it seemed okay as i displayed it.
– Jay Park
Jan 8 at 5:52
great, feel free to accept my answer ;)
– NiallJG
Jan 8 at 5:53
but actually i got one more problem, i saved my model and predicted. but result is all black(all zeros). do you know why this is occured?
– Jay Park
Jan 8 at 5:53
add a comment |
MNIST data is gray scale, which means it has a shape of (h,w,1) you have set PIL to convert the data to RGB which has three colour dimensions (h,w,3), the problem is with this line :
input_image = input_image.convert("RGB")
Either remove it entirely or replace it with
input_image = input_image.convert("L")
This should give your input data dimension (256,256,1)
input_image = Image.open(input_filename) input_image = np.array(input_image, dtype=np.float32) input_image = np.reshape(input_image, (256, 256, 1)) x_test.append(input_image) i've done by this way! it worked! is it fine?
– Jay Park
Jan 7 at 6:40
are you still getting the value error ? or is the model now running ? Reshaping should be ok on this dataset, I am not aware of any full color MNIST datasets so using grayscale or reshaping to (256, 256, 1) are equivalent. You can check this directly by loading up input_image and displaying it as a smoke test
– NiallJG
Jan 7 at 6:50
no it works now! and it seemed okay as i displayed it.
– Jay Park
Jan 8 at 5:52
great, feel free to accept my answer ;)
– NiallJG
Jan 8 at 5:53
but actually i got one more problem, i saved my model and predicted. but result is all black(all zeros). do you know why this is occured?
– Jay Park
Jan 8 at 5:53
add a comment |
MNIST data is gray scale, which means it has a shape of (h,w,1) you have set PIL to convert the data to RGB which has three colour dimensions (h,w,3), the problem is with this line :
input_image = input_image.convert("RGB")
Either remove it entirely or replace it with
input_image = input_image.convert("L")
This should give your input data dimension (256,256,1)
MNIST data is gray scale, which means it has a shape of (h,w,1) you have set PIL to convert the data to RGB which has three colour dimensions (h,w,3), the problem is with this line :
input_image = input_image.convert("RGB")
Either remove it entirely or replace it with
input_image = input_image.convert("L")
This should give your input data dimension (256,256,1)
answered Jan 4 at 10:34
NiallJGNiallJG
1,0601319
1,0601319
input_image = Image.open(input_filename) input_image = np.array(input_image, dtype=np.float32) input_image = np.reshape(input_image, (256, 256, 1)) x_test.append(input_image) i've done by this way! it worked! is it fine?
– Jay Park
Jan 7 at 6:40
are you still getting the value error ? or is the model now running ? Reshaping should be ok on this dataset, I am not aware of any full color MNIST datasets so using grayscale or reshaping to (256, 256, 1) are equivalent. You can check this directly by loading up input_image and displaying it as a smoke test
– NiallJG
Jan 7 at 6:50
no it works now! and it seemed okay as i displayed it.
– Jay Park
Jan 8 at 5:52
great, feel free to accept my answer ;)
– NiallJG
Jan 8 at 5:53
but actually i got one more problem, i saved my model and predicted. but result is all black(all zeros). do you know why this is occured?
– Jay Park
Jan 8 at 5:53
add a comment |
input_image = Image.open(input_filename) input_image = np.array(input_image, dtype=np.float32) input_image = np.reshape(input_image, (256, 256, 1)) x_test.append(input_image) i've done by this way! it worked! is it fine?
– Jay Park
Jan 7 at 6:40
are you still getting the value error ? or is the model now running ? Reshaping should be ok on this dataset, I am not aware of any full color MNIST datasets so using grayscale or reshaping to (256, 256, 1) are equivalent. You can check this directly by loading up input_image and displaying it as a smoke test
– NiallJG
Jan 7 at 6:50
no it works now! and it seemed okay as i displayed it.
– Jay Park
Jan 8 at 5:52
great, feel free to accept my answer ;)
– NiallJG
Jan 8 at 5:53
but actually i got one more problem, i saved my model and predicted. but result is all black(all zeros). do you know why this is occured?
– Jay Park
Jan 8 at 5:53
input_image = Image.open(input_filename) input_image = np.array(input_image, dtype=np.float32) input_image = np.reshape(input_image, (256, 256, 1)) x_test.append(input_image) i've done by this way! it worked! is it fine?
– Jay Park
Jan 7 at 6:40
input_image = Image.open(input_filename) input_image = np.array(input_image, dtype=np.float32) input_image = np.reshape(input_image, (256, 256, 1)) x_test.append(input_image) i've done by this way! it worked! is it fine?
– Jay Park
Jan 7 at 6:40
are you still getting the value error ? or is the model now running ? Reshaping should be ok on this dataset, I am not aware of any full color MNIST datasets so using grayscale or reshaping to (256, 256, 1) are equivalent. You can check this directly by loading up input_image and displaying it as a smoke test
– NiallJG
Jan 7 at 6:50
are you still getting the value error ? or is the model now running ? Reshaping should be ok on this dataset, I am not aware of any full color MNIST datasets so using grayscale or reshaping to (256, 256, 1) are equivalent. You can check this directly by loading up input_image and displaying it as a smoke test
– NiallJG
Jan 7 at 6:50
no it works now! and it seemed okay as i displayed it.
– Jay Park
Jan 8 at 5:52
no it works now! and it seemed okay as i displayed it.
– Jay Park
Jan 8 at 5:52
great, feel free to accept my answer ;)
– NiallJG
Jan 8 at 5:53
great, feel free to accept my answer ;)
– NiallJG
Jan 8 at 5:53
but actually i got one more problem, i saved my model and predicted. but result is all black(all zeros). do you know why this is occured?
– Jay Park
Jan 8 at 5:53
but actually i got one more problem, i saved my model and predicted. but result is all black(all zeros). do you know why this is occured?
– Jay Park
Jan 8 at 5:53
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54032486%2ferror-when-checking-target-expected-to-have-shape-256-256-1-but-got-array-w%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown