0

I'm currently learning Tensorflow. And the best way to do so is to get hands dirty right.

As an exercise Im training a model with some pokemon data found on kaggle.

After the model has been created and I check it with one of the pictures, that trained it, the result is always wrong.

Can someone light me up ?

root_dir = "./images"
read_files = os.path.join(root_dir)
file_names = os.listdir(read_files)
data_pok = {}

def build_image_labels():
    data = pd.read_csv("./pokemon.csv")
    data.head()
    for index, row in data.iterrows():
        name = row["Name"]
        type_one = row["Type1"]
        type_two = row["Type2"]
        data_pok[index] = {
            "index": index + 1,
            "name": name,
            "type_one": type_one,
            "type_two": type_two
        }

    type_one = data["Type1"].unique()
    ids = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]
    types_indices = dict(zip(type_one, ids))

    final_images = []
    final_pokemon = []
    count = 0

    for file in file_names:
        pokemon = data_pok[count]
        count += 1
        img = cv2.imread(os.path.join(root_dir, file))
        img = cv2.resize(img, (120, 120))
        final_images.append(np.array(img))
        final_pokemon.append(np.array(pokemon['index']-1))

    final_images = np.array(final_images, dtype=np.float32)/255.0
    final_pokemon = np.array(final_pokemon, dtype=np.float32).reshape(809, 1)

    print("After", final_images.shape, final_pokemon.shape)

    return final_images, final_pokemon, type_one

def build_model():
    model = Sequential([
        Flatten(input_shape=(120,120,3)),
        Dense(100, activation='relu'),
        Dense(100, activation='relu'),
        Dense(100, activation='relu'),
        Dense(809),
    ])
    model.summary()

    model.compile(
        optimizer='Adam',
        loss= keras.losses.SparseCategoricalCrossentropy(from_logits=True),
        metrics=['accuracy']
    )

    (images, pokemons, types) = build_image_labels()

    history = model.fit(images, pokemons, epochs=50)
    probability_model = Sequential([model, Softmax()])

    pokemon = cv2.imread('./images/mewtwo.png')
    pokemon = cv2.resize(pokemon, (120,120))
    pokemon_arr = np.array(pokemon, dtype=np.float32) / 255.0
    pokemon_arr = np.expand_dims(pokemon, axis=0)

    cv2.imshow("Pokemon", pokemon)
    cv2.waitKey(0)

    predictions = probability_model.predict(pokemon)

    id = np.argmax(predictions[0])
    print("id", id)
    print("pokemon index", data_pok[id])
    print("accuracy of the model", history.history['accuracy'][-1])

build_model()

#EDIT here is the link to the data I'm using from Kaggle. Pokemon data

4
  • if debugging doesn't help then maybe you could ask on similar portals DataScience, CrossValidated or forum Kaggle - they should have better experience in Machine Learning. Commented Apr 25 at 22:26
  • if you would add link to data on Kaggle then people could test code on data (and debug it) Commented Apr 25 at 22:29
  • @furas, I update the post with the link. Thanks I'm basically the index-1 of the pokemon into an array, there is not much to print. Indices are added against images files, read in the same order. Regarding the image, the code stops to display it with opencv2 - so I know which index-1 I expect Commented Apr 26 at 5:45
  • 1
    Hi @Ruben, The model is failing because 1) You are predicting on un-normalized pixel values (0-255) while the model was trained on normalized data (0-1). 2) The os.listdir() doesn't guarantee a sorted order, leading to mislabeled training data. 3) A Flatten layer is not suitable for image recognition as it ignores spatial relationships; instead a CNN is needed. 4) The lack of a validation split means the reported accuracy is likely misleading, as the model may just be memorizing the training images. Commented Sep 15 at 8:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.