Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 13951

I want to find "Vehicle Make and Model" by uploading the image

$
0
0
import deeplakefrom tensorflow import kerasfrom tensorflow.keras import layers

Load the training and testing subsets

train_dataset = deeplake.load("hub://activeloop/stanford-cars-train")test_dataset = deeplake.load("hub://activeloop/stanford-cars-test")

Create TensorFlow dataloaders

train_dataloader = train_dataset.tensorflow()test_dataloader = test_dataset.tensorflow()

Define constants

image_height, image_width, num_channels = (224, 224, 3)  # Adjust based on your datasetnum_classes = 196  # Number of car classes in the dataset

Assuming your dataset has an 'images' key

input_layer = layers.Input(shape=(image_height, image_width, num_channels), name='images')x = layers.Conv2D(32, (3, 3), activation='relu')(input_layer)x = layers.MaxPooling2D((2, 2))(x)x = layers.Conv2D(64, (3, 3), activation='relu')(x)x = layers.MaxPooling2D((2, 2))(x)x = layers.Conv2D(128, (3, 3), activation='relu')(x)x = layers.Flatten()(x)x = layers.Dense(256, activation='relu')(x)output_layer = layers.Dense(num_classes, activation='softmax', name='car_models')(x)model = keras.Model(inputs=input_layer, outputs=output_layer)

Compile the model

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

Specify the number of epochs and other training parameters

num_epochs = 10

Train the model

model.fit(train_dataloader, epochs=num_epochs, validation_data=test_dataloader)

Evaluate the model on the test set

test_loss, test_accuracy = model.evaluate(test_dataloader)print(f'Test Accuracy: {test_accuracy * 100:.2f}%')

Make predictions on new images

Assuming you have a new image (replace 'your_image_path' with the actual path)

from tensorflow.keras.preprocessing import imageimport numpy as npnew_image_path = '/content/bmw2.jpg'img = image.load_img(new_image_path, target_size=(image_height, image_width))img_array = image.img_to_array(img)img_array = np.expand_dims(img_array, axis=0)  # Add batch dimensionimg_array /= 255.0  # Normalize pixel values

Make prediction

predictions = model.predict(img_array)predicted_class = np.argmax(predictions[0])

Map the predicted class to the actual car make and model (you may need a mapping from class index to make and model)

class_mapping = {}  # Define your class mappingpredicted_make_model = class_mapping.get(predicted_class, "Unknown")print(f'The predicted make and model for the given image is: {predicted_make_model}')

Viewing all articles
Browse latest Browse all 13951

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>