[Python] Tensorflow - Trained model always wrong, on image it trained on

Discussão em 'Python' iniciado por Stack, Abril 25, 2025.

  1. Stack

    Stack Membro Participativo

    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()

    Continue reading...

Compartilhe esta Página