0

I'm trying to do real time gesture recognition but I'm new to tensorflow and keras so I took this code as a base https://github.com/CircuitDigest/Rock-Paper-Scissors-with-Pi/blob/main/training .py I installed keras and tensorflow but when I run the script I get this error:

Traceback (most recent call last):
  File "C:/Users/belog/drone_sees/train_model.py", line 61, in <module>
    model = get_model()
  File "C:/Users/belog/drone_sees/train_model.py", line 30, in get_model
    SqueezeNet(input_shape=(227, 227, 3), include_top=False),
  File "C:\Users\belog\drone_sees\venv\lib\site-packages\keras_squeezenet\squeezenet.py", line 133, in SqueezeNet
    model.load_weights(weights_path)
  File "C:\Users\belog\drone_sees\venv\lib\site-packages\keras\engine\topology.py", line 2622, in load_weights
    load_weights_from_hdf5_group(f, self.layers)
  File "C:\Users\belog\drone_sees\venv\lib\site-packages\keras\engine\topology.py", line 3089, in load_weights_from_hdf5_group
    original_keras_version = f.attrs['keras_version'].decode('utf8')
AttributeError: 'str' object has no attribute 'decode'

Process finished with exit code 1

Here is my script:

import cv2
import numpy as np
from keras_squeezenet import SqueezeNet
from keras.optimizers import Adam
from keras.utils import np_utils
from keras.layers import Activation, Dropout, Convolution2D, GlobalAveragePooling2D
from keras.models import Sequential
import tensorflow as tf
import os

IMG_SAVE_PATH = 'images'

CLASS_MAP = {
    "forward": 0,
    "back": 1,
    "up": 2,
    "down": 3,
    "stop": 4
}

NUM_CLASSES = len(CLASS_MAP)


def mapper(val):
    return CLASS_MAP[val]


def get_model():
    model = Sequential([
        SqueezeNet(input_shape=(227, 227, 3), include_top=False),
        Dropout(0.5),
        Convolution2D(NUM_CLASSES, (1, 1), padding='valid'),
        Activation('relu'),
        GlobalAveragePooling2D(),
        Activation('softmax')
    ])
    return model


dataset = []
for directory in os.listdir(IMG_SAVE_PATH):
    path = os.path.join(IMG_SAVE_PATH, directory)
    if not os.path.isdir(path):
        continue
    for item in os.listdir(path):
        if item.startswith("."):
            continue
        img = cv2.imread(os.path.join(path, item))
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        img = cv2.resize(img, (227, 227))
        dataset.append([img, directory])

data, labels = zip(*dataset)
labels = list(map(mapper, labels))


# one hot encode the labels
labels = np_utils.to_categorical(labels)

# define the model
model = get_model()
model.compile(
    optimizer=Adam(lr=0.0001),
    loss='categorical_crossentropy',
    metrics=['accuracy']
)

model.fit(np.array(data), np.array(labels), epochs=15)

model.save("gestures_model.h5")

What am I doing wrong and how to fix this error? (I use tensorflow==1.14.0 and keras==2.1.1)

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
Hallteon
  • 57
  • 2
  • 11
  • 2
    This error message generally happens because you are trying to use code that is intended to be run on a 2.x version of Python, on a 3.x version. Keras and Tensorflow are definitely available for 3.x, so this is probably some problem with the installation process. – Karl Knechtel Jan 21 '22 at 14:08
  • Does https://stackoverflow.com/questions/53740577/does-any-one-got-attributeerror-str-object-has-no-attribute-decode-whi solve the problem? – Karl Knechtel Jan 21 '22 at 14:13

0 Answers0