首页 文章

numpy:将图像形状从224 x 224 x 3更改为3 x 224 x 224的最快方法

提问于
浏览
0

我将使用Keras预训练的Inception V3模型 . 预处理后,图像形状为224 x 224 x 3.但是Keras Inception V3模型的输入是(?,3,?,?),即批量大小到达通道之后 . 所以我做了数组重塑 . 但这会使整个事情变得非常缓慢并且耗尽记忆力我不知道为什么 .

注意:当图像形状为224,224,3时,它在简单的CNN上工作正常 . 但3,224,224馈送到简单的CNN使得事情超级缓慢和内存溢出 .

这是我的代码:

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

2 回答

  • 0

    你可以使用.transpose

    im = im.transpose(2,0,1)
    

    所以从现在开始,旧的第三个索引( 2 )是第一个索引,旧的第一个索引( 0 )是第二个索引,旧的第二个索引( 1 )是第三个索引 .

    所以如果你访问 im[i,j,k] 就像你在转置之前访问过 im[j,k,i] 一样 .

  • 1

    除了 reshapetranspose 之外,另一个类似的解决方案:Numpy库中的 swapaxes . 以下行将第一个轴与数组 im 中的第三个轴交换 .

    im.swapaxes(0,2)
    

    如果a是ndarray,则返回a的视图;否则会创建一个新数组 . - 引自numpy-1.13.0 Docs

    参考

    How does numpy.swapaxes work?

相关问题