import pandas as pd
import numpy as np
import tensorflow as tf
import seaborn as sns
import matplotlib.pyplot as plt
import cv2
import os
import shutil
import random
from skimage.io import imread
from skimage.transform import rescale
from skimage.feature import hog
from sklearn.metrics import confusion_matrix, classification_report,accuracy_score
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Model, Sequential, load_model
from tensorflow.keras.layers import Dense, Flatten, BatchNormalization, Dropout, Conv2D, MaxPooling2D
from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau, CSVLogger
from tensorflow.keras.applications import resnet50, InceptionV3, DenseNet201
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.mobilenet import preprocess_input
from tensorflow.keras.preprocessing.image import load_img, img_to_array
sns.set_style('whitegrid')
sns.set()
%matplotlib inline
import os
data_dir = "/kaggle/input/gtsrb-german-traffic-sign/train"
image_paths = []
labels = []
classes = os.listdir(data_dir)
for class_name in classes:
class_dir = os.path.join(data_dir, class_name)
if os.path.isdir(class_dir):
files = os.listdir(class_dir)
for file in files:
image_paths.append(os.path.join(class_dir, file))
labels.append(class_name)
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import cv2
import tensorflow as tf
from tensorflow import keras
from PIL import Image
import os
import pathlib
import random as rn
from sklearn.model_selection import train_test_split
import tensorflow_hub as hub
from tensorflow.keras.preprocessing.image import ImageDataGenerator, img_to_array, array_to_img, load_img
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.models import Sequential, load_model
from tensorflow.keras.applications import VGG19
from tensorflow.keras.layers import Conv2D, MaxPool2D, Dense, Flatten, Dropout
from sklearn.metrics import accuracy_score
print("Num GPUs Available: ", len(tf.config.list_physical_devices('GPU')))
data_dir = '../input/gtsrb-german-traffic-sign/'
train_path = '../input/gtsrb-german-traffic-sign/Train/'
test_path = '../input/gtsrb-german-traffic-sign/Test/'
height = 50
width = 50
classes = { 0:'Speed limit (20km/h)',
1:'Speed limit (30km/h)',
2:'Speed limit (50km/h)',
3:'Speed limit (60km/h)',
4:'Speed limit (70km/h)',
5:'Speed limit (80km/h)',
6:'End of speed limit (80km/h)',
7:'Speed limit (100km/h)',
8:'Speed limit (120km/h)',
9:'No passing',
10:'No passing veh over 3.5 tons',
11:'Right-of-way at intersection',
12:'Priority road',
13:'Yield',
14:'Stop',
15:'No vehicles',
16:'Veh > 3.5 tons prohibited',
17:'No entry',
18:'General caution',
19:'Dangerous curve left',
20:'Dangerous curve right',
21:'Double curve',
22:'Bumpy road',
23:'Slippery road',
24:'Road narrows on the right',
25:'Road work',
26:'Traffic signals',
27:'Pedestrians',
28:'Children crossing',
29:'Bicycles crossing',
30:'Beware of ice/snow',
31:'Wild animals crossing',
32:'End speed + passing limits',
33:'Turn right ahead',
34:'Turn left ahead',
35:'Ahead only',
36:'Go straight or right',
37:'Go straight or left',
38:'Keep right',
39:'Keep left',
40:'Roundabout mandatory',
41:'End of no passing',
42:'End no passing veh > 3.5 tons' }
train_datagen = ImageDataGenerator(rescale=1./255,
validation_split=0.2)
train_dataset = train_datagen.flow_from_directory(train_path,
target_size=(height, width),
batch_size=batch_size,
class_mode='categorical',
shuffle=True,
seed=seed,
color_mode='rgb',
interpolation='hamming',
subset='training')
test_datagen = ImageDataGenerator(rescale=1./255,
validation_split=0.2)
test_dataset = test_datagen.flow_from_directory(train_path,
target_size=(height, width),
batch_size=batch_size,
class_mode='categorical',
shuffle=True,
seed=seed,
color_mode='rgb',
interpolation='hamming',
subset='validation')alpha=0.01
img, y = next(train_dataset)
for i in range(3):
for j in range(4):
l = np.random.randint(0, batch_size - 1)
label = classes[np.argmax(y[l])]
ax[i, j].imshow(img[l])
ax[i, j].set_title(label)
plt.tight_layout()
plt.show()
from tensorflow import keras
model = keras.models.Sequential([
keras.layers.Input(shape=(height, width, 3)), # Define input shape here
keras.layers.Conv2D(filters=16, kernel_size=(5, 5), activation='relu'),
keras.layers.Conv2D(filters=32, kernel_size=(5, 5), activation='relu'),
keras.layers.MaxPool2D(pool_size=(2, 2)),
keras.layers.BatchNormalization(axis=-1),
keras.layers.Conv2D(filters=64, kernel_size=(3, 3), activation='relu'),
keras.layers.Conv2D(filters=64, kernel_size=(3, 3), activation='relu'),
keras.layers.MaxPool2D(pool_size=(2, 2)),
keras.layers.BatchNormalization(axis=-1),
keras.layers.Dropout(rate=0.25),
keras.layers.Flatten(),
keras.layers.Dense(512, activation='relu'),
keras.layers.BatchNormalization(),
keras.layers.Dropout(rate=0.25),
keras.layers.Dense(43, activation='softmax')
])steps_per_epoch = train_dataset.samples // batch_size
validation_steps = test_dataset.samples // batch_size
epochs=15
optim = keras.optimizers.Adam(learning_rate=0.01)
model.compile(optimizer = optim, loss = 'categorical_crossentropy', metrics = ['accuracy'])
fig, ax = plt.subplots(3, 4)
fig.set_size_inches(16, 12)
history = model.fit(
train_dataset,
steps_per_epoch=steps_per_epoch,
epochs=epochs,
validation_data=test_dataset,
validation_steps=validation_steps
)
There are no datasets linked
There are no datasets linked