简体   繁体   中英

Python deep learning- training image

I'm trying to train a single image into my.network. As shown in the code below, I've set my input layer like that since size of single image I am trying to use is (256,256,3).

Here's the problem. I don't know reason but when I use model.summary() code, it keeps showing that input layer shape is "(None, 32, 256, 3) not (None, 256, 256, 3)". Anyone knows why "IMG_WIDTH" is set to 32 not 256?

import tensorflow as tf
IMG_WIDTH = 256
IMG_HEIGHT = 256
IMG_CHANNELS = 3

#define input
inputs = tf.keras.layers.Input(shape=(IMG_WIDTH, IMG_HEIGHT, IMG_CHANNELS))
c1_1 = tf.keras.layers.Conv2D(64,(3,3),activation='relu', padding='same')(inputs)

Try this example and you can find the solution using this example:

# Recognition & Classification of Images using Deep Learning Algorithms     
# We start coding by importing our libraries..
import tensorflow as tf
from tensorflow.keras import datasets, layers, models
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt


# #### We load our CIFAR10 dataset: (You must have an Internet connection for the download process). If you do not have a connection, you can also download the dataset from the Internet.


(X_train, y_train), (X_test,y_test) = datasets.cifar10.load_data()        

X_train.shape    

# #### Our array is like this because each photo has a square size of 32 pixels-32 pixels and has 3 channel RGB information in color.
# 
# <IMG src="cifar10_images.jpg" width="400" height="400">
# 

X_test.shape

y_train[:3]

# y_train and y_test are kept as a 2-dimensional array in the cifar10 dataset.
# We make this data one-dimensional to understand it visually more easily.
# We use reshape() to make a 2-dimensional array one-dimensional.

        
y_test = y_test.reshape(-1,)       

y_test 


# #### Let's take a look at the data. for this purpose we create an array ourselves:


image_classes = ["airplane","automobile","bird","cat","deer","dog","frog","horse","ship","truck"]


def plot_sample(X, y, index):
    plt.figure(figsize = (15,2))
    plt.imshow(X[index])        
    plt.xlabel(image_classes[y[index]])
    plt.show()


plot_sample(X_test, y_test, 0)

plot_sample(X_test, y_test, 1)

plot_sample(X_test, y_test, 3)


# ### Normalization
# 
# We need to normalize our data. Otherwise, CNN algorithms may give wrong results. Since the photos have 3 channels in RGB and each pixel has a value between 0-255, it is enough to simply divide each pixel value by 255 for normalization.



X_train = X_train / 255
X_test = X_test / 255


# ### We are designing our Deep Learning Algorithm using Convolutional Neural Network:

# <IMG src="deep7.png" width="800" height="400">
   
deep_learning_model = models.Sequential([
    # The first part is the Convolution layer..
    # In this part, we extract the features from the photos to be able to identify them...
    layers.Conv2D(filters=32, kernel_size=(3, 3), activation='relu', input_shape=(32, 32, 3)),
    layers.MaxPooling2D((2, 2)),
    
    layers.Conv2D(filters=64, kernel_size=(3, 3), activation='relu'),
    layers.MaxPooling2D((2, 2)),
    
    # The second part is a classical Articial Neural Network layer.
    # We will train our ANN model according to the above features and training information.
    layers.Flatten(),
    layers.Dense(64, activation='relu'),
    layers.Dense(10, activation='softmax')
])


deep_learning_model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])


# ### Let's start training our model now...


deep_learning_model.fit(X_train, y_train, epochs=5)
   
deep_learning_model.evaluate(X_test,y_test)
      
y_pred = deep_learning_model.predict(X_test)
y_pred[:3]    

y_predictions_siniflari = [np.argmax(element) for element in y_pred]
y_predictions_siniflari[:3]



y_test[:3]

plot_sample(X_test, y_test,0)

image_classes[y_predictions_siniflari[0]]

plot_sample(X_test, y_test,1)

image_classes[y_predictions_siniflari[1]]
    
plot_sample(X_test, y_test,2)

image_classes[y_predictions_siniflari[2]]

plot_sample(X_test, y_test,12)

image_classes[y_predictions_siniflari[12]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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