Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is TensorFlow2's CNN image classification method?

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the "TensorFlow2 CNN image classification method is what", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "TensorFlow2 CNN image classification method is what" it!

1. Guide package

Import matplotlib.pyplot as plt

Import numpy as np

Import pandas as pd

Import tensorflow as tf

From sklearn.preprocessing import StandardScaler

From sklearn.model_selection import train_test_split

two。 Image classification fashion_mnist

Data processing.

# Raw data

(X_train_all, y_train_all), (X_test, y_test) = tf.keras.datasets.fashion_mnist.load_data ()

# split training set and verification set

X_train, X_valid, y_train, y_valid = train_test_split (X_train_all, y_train_all, test_size=0.25)

# data standardization, you can also normalize by dividing by 255

# Note the 1 in the last reshape, which means that the image has only one channel, that is, the current image is a grayscale image

Scaler = StandardScaler ()

X_train_scaled = scaler.fit_transform (X_train.reshape (- 1,28 * 28)). Reshape (- 1,28,28,1)

X_valid_scaled = scaler.transform (X_valid.reshape (- 1,28 * 28)). Reshape (- 1,28,28,1)

X_test_scaled = scaler.transform (X_test.reshape (- 1,28 * 28)). Reshape (- 1,28,28,1)

Build CNN model

Model = tf.keras.models.Sequential ()

# multiple convolution layers

Model.add (tf.keras.layers.Conv2D (filters=32, kernel_size= [5,5], padding= "same", activation= "relu", input_shape= (28,28,1)

Model.add (tf.keras.layers.MaxPool2D (pool_size= [2,2], strides=2))

Model.add (tf.keras.layers.Conv2D (filters=64, kernel_size= [5,5], padding= "same", activation= "relu"))

Model.add (tf.keras.layers.MaxPool2D (pool_size= [2,2], strides=2))

# convert the multi-dimensional data obtained from the previous convolution layer into one dimension

# 7 is related to the previous kernel_size, padding and MaxPool2D

# Conv2D: 2828-> 2828 (because of padding= "same")

# MaxPool2D: 28028-> 14014

# Conv2D: 14014-> 14014 (because of padding= "same")

# MaxPool2D: 14: 14-> 7: 7

Model.add (tf.keras.layers.Reshape (target_shape= (7 * 7 * 64,)

# pass in the full connection layer

Model.add (tf.keras.layers.Dense (1024, activation= "relu"))

Model.add (tf.keras.layers.Dense (10, activation= "softmax")

# compile

Model.compile (loss = "sparse_categorical_crossentropy"

Optimizer = "sgd"

Metrics = ["accuracy"])

Model training

Callbacks = [

Tf.keras.callbacks.EarlyStopping (min_delta=1e-3, patience=5)

]

History = model.fit (X_train_scaled, y_train, epochs=15

Validation_data= (X_valid_scaled, y_valid)

Callbacks = callbacks)

Train on 50000 samples, validate on 10000 samples

Epoch 1/15

50000Universe 50000 [=]-17s 343us/sample-loss: 0.5707-accuracy: 0.7965-val_loss: 0.4631-val_accuracy: 0.8323

Epoch 2/15

50000Universe 50000 [=]-13s 259us/sample-loss: 0.3728-accuracy: 0.8669-val_loss: 0.3573-val_accuracy: 0.8738

...

Epoch 13/15

50000Universe 50000 [=]-12s 244us/sample-loss: 0.1625-accuracy: 0.9407-val_loss: 0.2489-val_accuracy: 0.9112

Epoch 14/15

50000Universe 50000 [=]-12s 240us/sample-loss: 0.1522-accuracy: 0.9451-val_loss: 0.2584-val_accuracy: 0.9104

Epoch 15/15

50000Universe 50000 [=]-12s 237us/sample-loss: 0.1424-accuracy: 0.9500-val_loss: 0.2521-val_accuracy: 0.9114

Draw a picture

Def plot_learning_curves (history):

Pd.DataFrame (history.history) .plot (figsize= (8,5))

Plt.grid (True)

# plt.gca () .set_ylim (0,1)

Plt.show ()

Plot_learning_curves (history)

Evaluation accuracy of test set

Model.evaluate (X_test_scaled, y_test)

[0.269884311157465, 0.9071]

It can be seen that the accuracy of image classification is significantly improved after using CNN. The previous model was 0.8747, now it is 0.9071.

3. Image classification Dogs vs. Cats

3.1 Raw data

Raw data download

Kaggle: https://www.kaggle.com/c/dogs-vs-cats/

Baidu network disk: https://pan.baidu.com/s/13hw4LK8ihR6-6-8mpjLKDA extraction code dmp4

Read a picture and show it

Image_string = tf.io.read_file ("C:/Users/Skey/Downloads/datasets/cat_vs_dog/train/cat.28.jpg")

Image_decoded = tf.image.decode_jpeg (image_string)

Plt.imshow (image_decoded)

3.2 use Dataset to load pictures

Because there are too many original pictures, we cannot load all the pictures into memory at once. Tensorflow provides us with a convenient Dataset API, which can load data in batches from the hard disk for training.

Deal with local image paths and tags

# the path of training data

Train_dir = "C:/Users/Skey/Downloads/datasets/cat_vs_dog/train/"

Train_filenames = [] # File names of all pictures

Train_labels = [] # tags for all pictures

For filename in os.listdir (train_dir):

Train_filenames.append (train_dir + filename)

If (filename.startswith ("cat")):

Train_labels.append (0) # Mark cat as 0

Else:

Train_labels.append (1) # Mark dog as 1

# data randomly split the flow of people in Zhengzhou which hospital did a good http://www.csyhjlyy.com/?

X_train, X_valid, y_train, y_valid = train_test_split (train_filenames, train_labels, test_size=0.2)

Define a way to decode a picture

Def _ decode_and_resize (filename, label):

Image_string = tf.io.read_file (filename) # read pictures

Image_decoded = tf.image.decode_jpeg (image_string) # Decoding

Image_resized = tf.image.resize (image_decoded, [256,256]) / 255.0 # reset size and normalize

Return image_resized, label

Define Dataset for loading picture data

# training set

Train_dataset = tf.data.Dataset.from_tensor_slices ((train_filenames, train_labels))

Train_dataset = train_dataset.map (

Map_func=_decode_and_resize, # calls the previously defined method, parses the filename, and converts it to features and tags

Num_parallel_calls=tf.data.experimental.AUTOTUNE)

Train_dataset = train_dataset.shuffle (buffer_size=128) # set the buffer size

Train_dataset = train_dataset.batch (32) # amount of data per batch

Train_dataset = train_dataset.prefetch (tf.data.experimental.AUTOTUNE) # start preloading pictures, that is, CPU will load data from disk in advance, so you don't have to wait until after the last training.

# validation set

Valid_dataset = tf.data.Dataset.from_tensor_slices ((valid_filenames, valid_labels))

Valid_dataset = valid_dataset.map (

Map_func=_decode_and_resize

Num_parallel_calls=tf.data.experimental.AUTOTUNE)

Valid_dataset = valid_dataset.batch (32)

Build a CNN model and train it

Building models and compiling

Model = tf.keras.Sequential ([

# convolution, 32 filter (convolution cores), each with a size of 3 to 3 and a step size of 1

Tf.keras.layers.Conv2D (32, 3, activation='relu', input_shape= (256,256,3))

# pooling, default size 2'2, step size 2

Tf.keras.layers.MaxPooling2D ()

Tf.keras.layers.Conv2D (32, 5, activation='relu')

Tf.keras.layers.MaxPooling2D ()

Tf.keras.layers.Flatten ()

Tf.keras.layers.Dense (64, activation='relu')

Tf.keras.layers.Dense (2, activation='softmax')

])

Model.compile (

Optimizer=tf.keras.optimizers.Adam (learning_rate=0.001)

Loss=tf.keras.losses.sparse_categorical_crossentropy

Metrics= [tf.keras.metrics.sparse _ categorical_accuracy]

)

Model overview

Model.summary ()

Model: "sequential_1"

_

Layer (type) Output Shape Param #

=

Conv2d_2 (Conv2D) (None, 254,254,32) 896

_

Max_pooling2d_2 (MaxPooling2 (None, 127,127,32) 0

_

Conv2d_3 (Conv2D) (None, 123,123,32) 25632

_

Max_pooling2d_3 (MaxPooling2 (None, 61, 61, 32) 0

_

Flatten_1 (Flatten) (None, 119072) 0

_

Dense_2 (Dense) (None, 64) 7620672

_

Dense_3 (Dense) (None, 2) 130

=

Total params: 7647330

Trainable params: 7647330

Non-trainable params: 0

Start training.

Model.fit (train_dataset, epochs=10, validation_data=valid_dataset)

Due to the large amount of data, the training time here is long.

It is important to note that the step printed here, each step refers to a batch (for example, 32 samples of a batch)

Model evaluation

Test_dataset = tf.data.Dataset.from_tensor_slices ((valid_filenames, valid_labels))

Test_dataset = test_dataset.map (_ decode_and_resize)

Test_dataset = test_dataset.batch (32)

Print (model.metrics_names)

Print (model.evaluate (test_dataset))

Thank you for your reading, these are the contents of "what is TensorFlow2's CNN image classification method?" after the study of this article, I believe you have a deeper understanding of what TensorFlow2's CNN image classification method is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report