Sentdex / pygta5

Explorations of Using Python to play Grand Theft Auto 5.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ModuleNotFoundError: No module named 'tensorflow.contrib'

mHamzaArain opened this issue · comments

i have tried several times with tensorflow = 2.0.1 which throws error No module named 'tensorflow.contrib' when i installed tf=<1.1.5 i raised error no AttributeError: module 'tensorflow' has no attribute 'compat'

install tensorflow 1.15

@Disty0 i found solution that i have installed tensorflow 1.4.0rc1 which is compatible with tflearn 0.3.2(having 3d convolutional layer) in python 3.5 (or you can use 3.6) with it.
But i want to work with newer version of tensorflow as speed and efficiency does matter and i have used contrib code but still raise many errors

I switched to Keras for same reason. I am using InceptionV3 model for keras and modified the training code. It seems to work fine for now.

@Disty0 can you share your code

This is the code i use:

#Modified code of Sentex Pygta5 2. train_model.py

import numpy as np
import cv2
import time
import os
import pandas as pd
from collections import deque
from random import shuffle
import pickle

import tensorflow as tf
physical_devices = tf.config.list_physical_devices('GPU')
tf.config.experimental.set_memory_growth(physical_devices[0],True)

#os.environ["KERAS_BACKEND"] = "plaidml.keras.backend"

from keras import Input
from keras.applications.inception_v3 import InceptionV3
from keras.callbacks import TensorBoard
from keras.models import load_model

BATCH = 16

balanced_train_path = 'balanced_train_data/'

FILE_I_END = -1 #-1 for auto detect #Note: starting file is training_data-0.npy not training_data-1.npy

WIDTH = 480
HEIGHT = 270
EPOCHS = 1000000000 #i am too lazy to put it on while true

MODEL_NAME = 'ETS2_RAI-{}'.format('InceptionV3')

LOAD_MODEL = True

if FILE_I_END == -1:
    FILE_I_END = 0
    while True:
        file_name = balanced_train_path + 'training_data-{}.npy'.format(FILE_I_END)

        if os.path.isfile(file_name):
            print('File exists: ',FILE_I_END)
            FILE_I_END += 1
        else:
            FILE_I_END -= 1
            print('Final File: ',FILE_I_END) 
            break

input_tensor = Input(shape=(WIDTH,HEIGHT,3))
model = InceptionV3(include_top=True, input_tensor=input_tensor , pooling='max', classes=9, weights=None)
model.compile('Adagrad', 'categorical_crossentropy')

"""
tensorboard = TensorBoard(
    log_dir='logs', histogram_freq=0, write_graph=True, write_images=True,
    update_freq='epoch', profile_batch=2, embeddings_freq=0,
    embeddings_metadata=None
)
"""

if LOAD_MODEL:
    model = load_model(MODEL_NAME)
    print('We have loaded a previous model!')
    
# iterates through the training files
for e in range(EPOCHS):
    data_order = [i for i in range(0,FILE_I_END+1)]
    shuffle(data_order)
    for count,i in enumerate(data_order):
        
        try:
            file_name = balanced_train_path + 'training_data-{}.npy'.format(i)
            # full file info
            train_data = np.load(file_name, allow_pickle=True)

            SAMPLE = len(train_data)
            print('training_data-{}.npy - Sample Size: {} - Batch Size: {}'.format(i,SAMPLE,BATCH))
            
            X = np.array([i[0] for i in train_data])#.reshape(-1,WIDTH,HEIGHT,3) #Pre reshaped at recording
            Y = np.array([i[1] for i in train_data])
            
            print("============================")
            print("Epochs: {} - Steps: {}".format(e, count))
            model.fit(X, Y, batch_size=BATCH ,epochs=1, validation_split=0.02) #, callbacks=[tensorboard])
            print("============================")

            if count%5 == 0 and count != 0:
                print('SAVING MODEL!')
                model.save(MODEL_NAME)
                    
        except Exception as e:
            print(str(e))

print("FINISHED {} EPOCHS!".format(EPOCHS))

I just created a new model that works with tensorflow 2. For example, there is no error with like a basic CNN, or even xception

Solved