【发布时间】:2017-12-30 17:56:01
【问题描述】:
我将使用 Keras 预训练的 Inception V3 模型。预处理后的图像形状为 224 x 224 x 3。但 Keras Inception V3 模型的输入是 (?, 3 , ?, ?),即在批量大小之后进入通道。所以我做了数组重塑。但这使整个事情变得超级慢并且占用了内存,我不知道为什么。
注意:当图像形状为 224、224、3 时,它在简单的 CNN 上运行良好。但是输入简单的 CNN 的 3、224、224 会使事情变得超级慢并且内存溢出。
这是我的代码:
def get_image_preprocessed(image_name):
im = Image.open(image_name)
im = np.asarray(im)
im = im/float(255)
im = im.reshape(3,224,224) #this changes 224,224,3 to 3,224,224
return im
这是输入张量形状
tf.Tensor 'input_1:0' shape=(?, 3, ?, ?) dtype=float32
更多信息:
型号-
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(3,224, 224), padding='same', activation='relu', kernel_constraint=maxnorm(3)))
model.add(Dropout(0.2))
model.add(Conv2D(32, (3, 3), activation='relu', padding='same', kernel_constraint=maxnorm(3)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(512, activation='relu', kernel_constraint=maxnorm(3)))
model.add(Dropout(0.5))
model.add(Dense(3, activation='softmax'))
生成器函数-
def generator(save_dir_path, encoding_list, batch_size, image_size):
# Create empty arrays to contain batch of features and labels#
batch_features = np.zeros((batch_size, 3, image_size, image_size))
batch_labels = np.zeros((batch_size,len(encoding_list)))
image_list= [file for file in os.listdir(save_dir_path) if (file.endswith('.jpeg') or file.endswith('.-'))]
while True:
for i in range(batch_size):
# choose random index in features
image_name= random.choice(image_list)
batch_features[i] = get_image_preprocessed(save_dir_path, image_name)
batch_labels[i] = np.asarray(get_encoding(encoding_list, image_name.split('_')[0]))
yield batch_features, batch_labels
【问题讨论】:
-
这看起来像是
transpose问题,而不是reshape问题。3应继续表示通道,224,224应继续表示图像形状。 -
我做了,问题没有解决。我在这里添加了更多信息。
-
问题出在后端。您需要阅读文档以正确使用此模型。