How to use TensorFlow to classify images of clothing

Posted by


In this tutorial, we will go through the process of using TensorFlow to classify clothing images. TensorFlow is an open-source machine learning library developed by Google that allows you to build and train complex neural network models. We will be using the Fashion MNIST dataset, which consists of 70,000 grayscale images of 10 different types of clothing items.

Step 1: Install TensorFlow and Import Libraries
First, you need to install TensorFlow by running the following command in your terminal:

pip install tensorflow

Next, import the necessary libraries in your Python script:

import tensorflow as tf
from tensorflow.keras.datasets import fashion_mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten

Step 2: Load and Preprocess the Data
Now, we will load the Fashion MNIST dataset and preprocess the data. The dataset is already split into training and testing sets, so we just need to load it:

(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

# Normalize the pixel values to be between 0 and 1
train_images, test_images = train_images / 255.0, test_images / 255.0

Step 3: Build the Neural Network Model
Next, we will build a simple neural network model using TensorFlow’s Keras API. We will use a Sequential model with two layers: a Flatten layer to reshape the input data, and a Dense layer with softmax activation to classify the clothing images into one of the 10 categories.

model = Sequential([
    Flatten(input_shape=(28, 28)),
    Dense(128, activation='relu'),
    Dense(10, activation='softmax')
])

Step 4: Compile and Train the Model
Now, we will compile the model with an optimizer, loss function, and metrics, and then train the model on the training data.

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

model.fit(train_images, train_labels, epochs=10, batch_size=32)

Step 5: Evaluate the Model
After training the model, we can evaluate its performance on the test data.

test_loss, test_acc = model.evaluate(test_images, test_labels)
print(f'Test accuracy: {test_acc}')

Step 6: Make Predictions
Finally, we can use the trained model to make predictions on new images.

predictions = model.predict(test_images)

# Print the predicted class for the first image
print(f'Predicted class: {tf.argmax(predictions[0])}')

That’s it! You have successfully used TensorFlow to classify clothing images using a neural network model. Feel free to experiment with different model architectures, hyperparameters, and optimization techniques to improve the performance of your model. TensorFlow offers a wide range of tools and resources to help you build and train complex machine learning models, so don’t hesitate to explore further. Happy coding!

0 0 votes
Article Rating

Leave a Reply

29 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@rohitsaravana910
19 days ago

can someone help me the link to Locating the code wont work?

@Pshorts786
19 days ago

can anyone pls respond me fast which alogrithm is working behind it?

@mahmoudrefaat6611
19 days ago

Thanks, You are really great teacher

@rdg8268
19 days ago

05:10 why 128 in first Dense layer? I didn't understand how he get that conclusion.

@camilotorres9835
19 days ago

I really appreciate the theatricality of this instructor, gotta love the enthusiasm!

@reoingstrada7892
19 days ago

Hello TensorFlow really liked your tutorial, could you please upload new link for "Instuctions for location code"? it's seem the page was removed.

@astronaut205
19 days ago

in 5:29 add also

import tensorflow.compat.v1 as tf

tf.disable_v2_behavior()
after importing Tensorflow and keras
If you are using TensorFlow 2 it would not recognize AdamOprimizer()

@julianleon8798
19 days ago

Hi TensorFlow (and anyone else who reads this), I am new to machine learning but I work in fashion. I was wondering if a model can be trained to classify clothing by fabric. Has anyone seen something like this?

@clearcontentment3695
19 days ago

Bag is a clothing item ?

@hemantvitthal3732
19 days ago

Great teaching tech

@АбылайАхметов-э9р
19 days ago

I have a problem with this line of code (train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

@wchuchung6971
19 days ago

I wonder if anyone is still answering any questions I have.

I tried to whole coding in google colab and when I came to
plt.xlabel(class_names[train_labels[i]])
plt.show()

the coding is in correct, but when I changed 'class_name' to 'train_labels', it works but the picture labels are in numbers, not text. Also when I continue to do the rest and found the accuracy is very low, only 10%.

ANYONE HAVE THE SAME PROBLEM?

@internationalscholarhw
19 days ago

Good 👍

@ErikBongers
19 days ago

Why 128 units, RELU and sparse_categorical_crossentropy ?

@javiersuarez8415
19 days ago

Hello, would you, recommend me a video or a web page that teach me how to convert or prepare pictures to use in a NN as you showed us on this video? I have some pictures of fruits and I want to try them.

@NeilWatt-r7r
19 days ago

Love this teacher!!! LOL XD

@AlexAcostaB
19 days ago

The most I just learn … an ankle boot XD

@justcloudi9466
19 days ago

Where can I get that shirt hmmmm?

@GagandeepSingh-qs2vh
19 days ago

Hey folks, I have created series on how to use custom dataset for image classification using TensorFlow.
https://www.youtube.com/watch?v=Bq0ID0wVbtQ

@DhananjaySharma-in
19 days ago

love it 🙂

29
0
Would love your thoughts, please comment.x
()
x