【发布时间】:2018-11-20 20:17:59
【问题描述】:
假设我有一个从 Instagram 下载的 图像(转换为 numpy 数组)列表,以及它们对应的赞和用户关注者 .假设我有一个 CNN 模型(在 Tensorflow 上使用 Keras),我在这些图像(200x200x3 numpy 数组)上进行训练,它试图预测一张图片将获得的赞数。
如果我想为这个模型提供每个图像对应的关注者作为第二个输入?
这是我目前的代码:
IMAGESIZE = (200, 200)
def create_model():
# create model and add layers
model = Sequential()
model.add(Conv2D(10, 5, 5, activation='relu',
input_shape=(IMAGESIZE[0], IMAGESIZE[1], 3)))
model.add(Conv2D(10, 5, 5, activation='relu'))
model.add(MaxPool2D((5, 5)))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(50))
model.add(Activation('relu'))
model.add(Dense(1))
print(model.summary())
model.compile(loss='mse',
optimizer='rmsprop', metrics=["accuracy"])
return model
# Read the likes
likes = getlikes(src='../data/pickledump')
likesraw = np.array(likes)
likes = (likesraw - np.mean(likesraw))/np.std(likesraw) # normalize
# Read the images and resize them
images = []
for imgfile in glob.glob('../data/download/*.jpeg'):
img = cv2.imread(imgfile)
resized = cv2.resize(img, IMAGESIZE)
images.append(resized)
break
images = np.array(images)
# Read the followers
followers= getfollowers(src='../data/pickledump')
followersraw= np.array(followers)
followers= (followersraw- np.mean(followersraw))/np.std(followersraw) # normalize
classifier = KerasClassifier(build_fn=create_model, epochs=20)
print("Accuracy (Cross Validation=10): ",
np.mean(cross_val_score(classifier, images, likes, cv=2)))
【问题讨论】:
标签: python tensorflow machine-learning keras conv-neural-network