Hello. I have a python script that is taking data from the GTSRB dataset from…
- User
Hello. I have a python script that is taking data from the GTSRB dataset from https://www.kaggle.com/datasets/meowmeowmeowmeowmeow/gtsrb-german-traffic-sign . I have this script attached that gets stuck at the end, after displaying the accuracy so it is not saving the .h5 model. I need some help with that.
- Assistant
Sure, I can help you with that. Can you share your Python script so I can take a look at it and see where the issue might be?
- User
import numpy as np import matplotlib.pyplot as plt from keras.models import Sequential from keras.layers import Dense from keras.optimizers import Adam from keras.utils.np_utils import to_categorical from keras.layers import Dropout, Flatten from keras.layers.convolutional import Conv2D, MaxPooling2D import cv2 from sklearn.model_selection import train_test_split import os import pandas as pd import random from keras.preprocessing.image import ImageDataGenerator from sklearn.metrics import confusion_matrix from tensorflow import keras import seaborn as sn
Reading images from directory
Using only the Train data from the GTSRB dataset
count = 0 images = [] label = [] classes_list = os.listdir("C:/Bachelor/GTSRB - German Traffic Sign Recognition Benchmark/Train") print("Total Classes:",len(classes_list)) noOfClasses=len(classes_list) print("Importing Classes.....") for x in range (0,len(classes_list)): imglist = os.listdir("C:/Bachelor/GTSRB - German Traffic Sign Recognition Benchmark/Train"+"/"+str(count)) for y in imglist: img = cv2.imread("C:/Bachelor/GTSRB - German Traffic Sign Recognition Benchmark/Train"+"/"+str(count)+"/"+y) img =cv2.resize(img,(32,32)) images.append(img) label.append(count) print(count, end =" ") count +=1 print(" ")
Reshaping the images to be 32x32
- Assistant
Based on the script you provided, it seems that the model training and evaluation pipeline is missing. This might be the reason why the script is not saving the .h5 model. Here's a template pipeline that you can use to train and evaluate the model using the GTSRB dataset:
# Splitting the dataset into training and testing subsets X_train, X_test, y_train, y_test = train_test_split(data, classNo, test_size=0.2, random_state=42) # Preprocessing the images X_train = X_train/255 X_test = X_test/255 y_train = to_categorical(y_train, noOfClasses) y_test = to_categorical(y_test, noOfClasses) # Defining the model architecture model = Sequential() model.add(Conv2D(32, (5,5), input_shape=(32,32,3), activation='relu')) model.add(MaxPooling2D(pool_size=(2,2))) model.add(Conv2D(64, (3,3), activation='relu')) model.add(MaxPooling2D(pool_size=(2,2))) model.add(Flatten()) model.add(Dense(512, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(noOfClasses, activation='softmax')) # Compiling the model model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=0.001), metrics=['accuracy']) # Data augmentation datagen = ImageDataGenerator(width_shift_range=0.1, height_shift_range=0.1, zoom_range=0.2, shear_range=0.1, rotation_range=10) # Training the model history = model.fit(datagen.flow(X_train, y_train, batch_size=64), epochs=50, validation_data=(X_test, y_test)) # Evaluating the model score = model.evaluate(X_test, y_test, verbose=0) print("Test Accuracy: {:.2f}%".format(score[1]*100)) # Saving the model model.save('traffic_sign_model.h5') - User
Hello. I have a python script that is taking data from the GTSRB dataset from https://www.kaggle.com/datasets/meowmeowmeowmeowmeow/gtsrb-german-traffic-sign . I have this script attached that gets stuck at the end, after displaying the accuracy so it is not saving the .h5 model. I need some help with that.
- Assistant
Hello! Can you please share your script so that I can take a look and help you out?
- User
- Assistant
It seems like the reason why the model is not being saved is because there is a typo in the filename. In the line
model.save('traffif_sign_model.h5'),'traffic_sign_model.h5'is misspelled as'traffif_sign_model.h5'. This results in an error preventing the model from being saved. Change the line tomodel.save('traffic_sign_model.h5')and see if that works. - User
misspelled nat a problem
- Assistant
In that case, can you try printing out the last line of the code (
model.save('traffif_sign_model.h5')) to make sure that it is being executed? You can also try changing'traffif_sign_model.h5'to an absolute path to make sure that the model is being saved in the right place. For example, you can change the line tomodel.save('/path/to/save/traffic_sign_model.h5'). - User
Error! c:\users\umair\traffif_sign_model.h5 is not UTF-8 encoded saving disabled. see console for more details.
- Assistant
This error occurs because the model file's path contains non-UTF-8 characters. To fix this error, you can change the path to only contain UTF-8 characters or use a different path to save your model file.
For example, you can use a path that only contains ASCII characters. Here's an example of how you can modify the save function to use a different path:
import os # Define the path to save the model model_file = 'traffic_sign_model.h5' save_path = os.path.join(os.getcwd(), model_file) # Save the model model.save(save_path)This changes the save path to the current working directory (
os.getcwd()) and concatenates it with the model file name. This should avoid non-UTF-8 characters in the path and allow you to save the model successfully. - User
also this problem
- Assistant
Could you please provide more context on the problem you're facing? Without more information, it's difficult to provide an accurate solution.
- User
Error! c:\users\umair\traffif_sign_model.h5 is not UTF-8 encoded saving disabled. see console for more details.
- Assistant
This error is caused by the file path to your output file not being UTF-8 encoded. One solution is to use pathlib to get the path to the output file as a WindowsPath object. Then you can create a string object that uses the raw string notation to set the unicode escape, which is able to handle non-ascii characters. Here's a modified version of your code that should work:
# Add this to the top of your code from pathlib import Path # Define the path to save the model model_file = 'traffic_sign_model.h5' save_path = Path.cwd() / model_file # Save the model model.save(rf"{save_path}")In this change, you define the save path using
Path.cwd()which returns aWindowsPathobject of the current working directory. You can then create a string object using a raw string literal (prefixing the string withr) which allows you to set the unicode escape character using{}syntax. Using the WindowsPath object like this ensures that the path is compatible with non-ascii characters. - User
also this problem
- Assistant
The error message
AttributeError: module 'tensorflow' has no attribute 'keras'occurs when there is a version incompatibility between TensorFlow and Keras.Starting from TensorFlow 2.4, Keras has been fully integrated into TensorFlow as the official high-level API. Therefore, you should use
tensorflow.kerasinstead ofkerasin the import statements for the Keras modules. If you're using an earlier version of TensorFlow, you should use standalone Keras instead oftensorflow.keras.To fix this error, you can modify your import statements to use
tensorflow.kerasinstead ofkeras. Here's an example of how to change your import statements:# Replace these import statements: from keras.layers import Dense, Dropout, Flatten from keras.layers.convolutional import Conv2D, MaxPooling2D from keras.models import Sequential from keras.optimizers import Adam from keras.utils.np_utils import to_categorical # With these: from tensorflow.keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D from tensorflow.keras.models import Sequential from tensorflow.keras.optimizers import Adam from tensorflow.keras.utils import to_categorical - User
Error! c:\users\umair\traffif_sign_model.h5 is not UTF-8 encoded saving disabled. see console for more details.
- Assistant
This error occurs because the model file's path contains non-UTF-8 characters. To fix this error, you can change the path to only contain UTF-8 characters or use a different path to save your model file.
For example, you can use a path that only contains ASCII characters. Here's an example of how you can modify the save function to use a different path:
import os # Define the path to save the model model_file = 'traffic_sign_model.h5' save_path = os.path.join(os.getcwd(), model_file) # Save the model model.save(save_path)This changes the save path to the current working directory (
os.getcwd()) and concatenates it with the model file name. This should avoid non-UTF-8 characters in the path and allow you to save the model successfully. - User
also this problem
- Assistant
The error message
AttributeError: module 'tensorflow' has no attribute 'keras'occurs when there is a version incompatibility between TensorFlow and Keras.Starting from TensorFlow 2.4, Keras has been fully integrated into TensorFlow as the official high-level API. Therefore, you should use
tensorflow.kerasinstead ofkerasin the import statements for the Keras modules. If you're using an earlier version of TensorFlow, you should use standalone Keras instead oftensorflow.keras.To fix this error, you can modify your import statements to use
tensorflow.kerasinstead ofkeras. Here's an example of how to change your import statements:# Replace these import statements: from keras.layers import Dense, Dropout, Flatten from keras.layers.convolutional import Conv2D, MaxPooling2D from keras.models import Sequential from keras.optimizers import Adam from keras.utils.np_utils import to_categorical # With these: from tensorflow.keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D from tensorflow.keras.models import Sequential from tensorflow.keras.optimizers import Adam from tensorflow.keras.utils import to_categorical - User
also this coding but also giving a problem
- Assistant
Please provide more details on the error or issue you're encountering. Without more information, I cannot provide an accurate solution.
- User
see previous data
- Assistant
I apologize, but I cannot find any previous question related to this issue. Can you please provide more information about the problem you're facing?
- User
Error! c:\users\umair\traffif_sign_model.h5 is not UTF-8 encoded saving disabled. see console for more details.
- Assistant
This error occurs because the model file's path contains non-UTF-8 characters. To fix this error, you can
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.