我正在尝试为我的图像分类制作CNN。 在开始之前,我正在尝试制作一个简单的神经网络模型。 但我有错误按摩,“不能为Tensor'Plankholder_30:0'提供形状值(1,224,224,3),其形状为'(1,150528)'” 我的图像大小都是不同的...所以我将所有图像大小调整为224 * 224 * 3并制作模型以对7个标签进行分类..但我遇到了这个错误,我不知道该怎么办。 这是我的代码请帮助
# -*- coding: utf-8 -*-
"""
Created on Fri Jan 12 14:46:07 2018
@author: Yoon seock ju
"""
#clear all the variable in spyder
%reset
#import packages
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from scipy import misc
import glob
import random
from numpy import array
#InteractiveSession ready
sess = tf.InteractiveSession()
#size for resize image data
size = 224,224
#train, test image list
trainset = []
trainlabel = []
testset = []
testlabel = []
#read train_image data
for i in range(7):
path = "C:/Users/user/Desktop/향수이미지/train/"+str(i)+"/*.jpg"
for image_path in glob.glob(path):
image = misc.imread(image_path)
print(image.shape)
if (image.shape[0] >224) and (image.shape[1] > 224):
print("resize start")
image = misc.imresize(image,size)
image = array(image).reshape(1, 224,224,3)
trainset.append(image)
trainlabel.append(i)
print("resize image= ",image.shape)
print(image.dtype)
print(i," is done")
#read test_image data
for i in range(7):
path = "C:/Users/user/Desktop/향수이미지/test/"+str(i)+"/*.jpg"
for image_path in glob.glob(path):
image = misc.imread(image_path)
print(image.shape)
if (image.shape[0] >224) and (image.shape[1] > 224):
print("resize start")
image = misc.imresize(image,size)
image = array(image).reshape(1,224,224,3)
testset.append(image)
testlabel.append(i)
print("resize image= ",image.shape)
print(image.dtype)
print(i," is done")
#set placeholder
x = tf.placeholder(tf.float32, shape = [1, 150528])#Our images 224*224*3
y_ = tf.placeholder(tf.float32, shape = [None, 7])#Our target
#set Variables
W = tf.Variable(tf.zeros([150528,7]))
b = tf.Variable(tf.zeros([7]))
#start session for initialize variables
sess.run(tf.global_variables_initializer())
#soft max & cost function
y = tf.nn.softmax(tf.matmul(x,W) + b)
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_*tf.log(y), reduction_indices=[1]))
#training the model
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
#run train_step
for i in range(1170):
train_step.run(feed_dict = {x:trainset[i], y_:trainlabel[i]})
答案 0 :(得分:0)
您可以更改
行image = array(image).reshape(1,224,224,3)
到
image = array(image).reshape(1,150528)
使您的数据形状与您的模型的输入形状相匹配。
答案 1 :(得分:0)
因此我将所有图像大小调整为224 * 224 * 3并制作模型以对7个标签进行分类
不,你没有,因为
image = array(image).reshape(1,224,224,3)
应该是
image = array(image).reshape(1, 224 * 224 * 3)
答案 2 :(得分:0)
针对可能正在使用PIL的其他人
image = Image.open(file_name))
image = image.convert('RGB')