ValueError: не удалось передать входной массив из фигуры (30,30,3) в фигуру (30,30) - PullRequest
0 голосов
/ 12 апреля 2020

При попытке обработать изображения с тензорным потоком я получаю следующее сообщение об ошибке. Я все еще учусь кодировать, но я делаю учебник по модели обучения изображений, и я использовал изображения автомобилей и футбола.

Я попытался изменить размеры изображений от 30 x 30 до 200 x 200 в коде и я все еще получаю ту же ошибку. У меня такое ощущение, что изображение сохраняется в виде двухмерного массива, и его необходимо преобразовать в трехмерный массив, и я не слишком уверен в этом.

    ValueError                                Traceback (most recent call last)
<ipython-input-11-fc2efd633014> in <module>()
     14 
     15             sess.run(training_operation, feed_dict={image_place:batch_x, label_place:batch_y})
---> 16             train_accuracy = sess.run(accuracy, feed_dict={image_place:X_train,label_place:Y_train})
     17             test_accuracy = sess.run(accuracy, feed_dict={image_place:X_eval, label_place:Y_eval})
     18 

~\Anaconda3\lib\site-packages\tensorflow_core\python\client\session.py in run(self, fetches, feed_dict, options, run_metadata)
    958     try:
    959       result = self._run(None, fetches, feed_dict, options_ptr,
--> 960                          run_metadata_ptr)
    961       if run_metadata:
    962         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

~\Anaconda3\lib\site-packages\tensorflow_core\python\client\session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1150             feed_handles[subfeed_t.experimental_ref()] = subfeed_val
   1151           else:
-> 1152             np_val = np.asarray(subfeed_val, dtype=subfeed_dtype)
   1153 
   1154           if (not is_tensor_handle_feed and

~\Anaconda3\lib\site-packages\numpy\core\_asarray.py in asarray(a, dtype, order)
     83 
     84     """
---> 85     return array(a, dtype, copy=False, order=order)
     86 
     87 

ValueError: could not broadcast input array from shape (30,30,3) into shape (30,30)

А это мой код:

import os
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
import numpy as np
from PIL import Image
from sklearn.utils import shuffle
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt

#create empty list
X=[]
Y=[]

string_to_number ={"car":0,"football":1}

car_folder ="Loading_data/cars"
football_folder ="loading_data/football"

#load data from one folder

def create_data(folder,name):
    for i in os.listdir(folder):
        image =Image.open(os.path.join(folder,i))
        image = Image.Image.resize(image, [30,30])

        x=np.array(image)
        X.append(x)
        Y.append(string_to_number[name])

create_data(car_folder,"car")
create_data(football_folder,"football")

#creating placeholder to feed the images
image_place = tf.placeholder(tf.float32, shape =[None,30,30,3]) 
label_place =  tf.placeholder(tf.int32, shape = [None,])

#converting labels to binary. One hot coding is coverting data to zeros and one, yes or no 
one_hot =tf.one_hot(label_place,2) 
one_hot = tf.cast(one_hot,tf.int32)

#Split the data into training and evaluation
X_train,X_eval,Y_train,Y_eval =train_test_split(X,Y, test_size=0.1) 

#turning the arrays into tensors. Essentially setting up the layers of the neural network
input_layer = tf.reshape(image_place, shape= [-1,30,30,3])  

flatten = tf.reshape(input_layer, [-1,(30*30*3)])
fc1 = tf.layers.dense(flatten, units =10, activation = tf.nn.relu)
fc2 = tf.layers.dense(fc1, units = 10, activation = tf.nn.relu)
fc3 = tf.layers.dense(fc2, units = 10, activation = tf.nn.relu)
dropout = tf.layers.dropout(fc3, rate=0.5)
logits = tf.layers.dense(dropout, units=2)

#loss minimisation
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits,labels = one_hot))
optimiser = tf.train.GradientDescentOptimizer(learning_rate=0.1)
training_operation = optimiser.minimize(loss)

correct_prediction = tf.equal(tf.argmax(one_hot,1),tf.argmax(logits,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))

# number of cycles
EPOCH_TOTAL = 20
BATCH_SIZE = 5

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    for each_epoch in range(EPOCH_TOTAL):
        X_train,Y_train = shuffle(X_train, Y_train)

        for batch_start in range(0, len(X_train), BATCH_SIZE):
            batch_end = batch_start + BATCH_SIZE
            batch_x, batch_y = X_train[batch_start:batch_end], Y_train[batch_start:batch_end]

            sess.run(training_operation, feed_dict={image_place:batch_x, label_place:batch_y})
            train_accuracy = sess.run(accuracy, feed_dict={image_place:X_train,label_place:Y_train}) 
            test_accuracy = sess.run(accuracy, feed_dict={image_place:X_eval, label_place:Y_eval})

            print("\nEpoch: {}".format(each_epoch))
            print("Batch: {} out of {}".format(batch_start, len(x_train)))
            print("--------------------")
            print("Train accuracy: {a:0.0f}").format(a=train_accuracy)
            print("Test accuracy: {a:0.8f}".format(a=test_accuracy))
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...