简体   繁体   English

如何将jpg图像加载到3d rgb numpy数组

[英]How to load a jpg image to a 3d rgb numpy array

I have made a tensorflow model and have trained and tested it on directories of image using the model.fit_generator method. 我已经制作了张量流模型,并使用model.fit_generator方法对图像目录进行了训练和测试。 But know I want to use it on a single image and there aren't any methods i can find that allow this so i decided to use numpy arrays by converting a jpg image to 3d rgb numpy array. 但是知道我想在单个图像上使用它并且没有任何方法我可以找到允许这样我所以我决定通过将jpg图像转换为3d rgb numpy数组来使用numpy数组。 How would you do this? 你会怎么做?

model = Sequential()
model.add(Convolution2D(32, 3, 3, input_shape=(img_width, img_height,3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Convolution2D(32, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Convolution2D(64, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))

model.compile(loss='binary_crossentropy',
          optimizer='rmsprop',
          metrics=['accuracy'])


nb_epoch = 1
nb_train_samples = 2048
nb_validation_samples = 832

#model.fit_generator(
#    train_generator,
#    samples_per_epoch=nb_train_samples,
#   nb_epoch=nb_epoch,
#   validation_data=validation_generator,   
#  nb_val_samples=nb_validation_samples)

Try using PIL (pip install Pillow): 尝试使用PIL(pip install Pillow):

from PIL import Image 
import numpy as np
im = Image.open("test.jpg")
im = np.array(im,dtype=np.float32)

And then to predict: 然后预测:

#Assuming batch size of 1 and data is normalised
y = model.predict(np.expand_dims(im/255,0))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM