Sublime text errors. (import?) - python

Tried to run this code in Sublime:
import tensorflow as tf
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten
from tensorflow.keras.layers import Conv2D, MaxPooling2D
import pickle
pickle_in = open("X.pickle","rb")
X = pickle.load(pickle_in)
pickle_in = open("y.pickle","rb")
y = pickle.load(pickle_in)
X = X/255.0
model = Sequential()
model.add(Conv2D(256, (3, 3), input_shape=X.shape[1:]))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(256, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten()) # this converts our 3D feature maps to 1D feature vectors
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.fit(X, y, batch_size=32, epochs=10, validation_split=0.3)
but I keep getting those errors:
Traceback (most recent call last):
File "D:\catsdogsai\catsdogsai\catsdogsai.py", line 1, in <module>
import tensorflow as tf
File "C:\Users\Edward\Anaconda3\envs\aitest\lib\site-packages\tensorflow\__init__.py", line 28, in <module>
from tensorflow.python import pywrap_tensorflow # pylint: disable=unused-import
File "C:\Users\Edward\Anaconda3\envs\aitest\lib\site-packages\tensorflow\python\__init__.py", line 47, in <module>
import numpy as np
File "C:\Users\Edward\Anaconda3\envs\aitest\lib\site-packages\numpy\__init__.py", line 140, in <module>
from . import _distributor_init
File "C:\Users\Edward\Anaconda3\envs\aitest\lib\site-packages\numpy\_distributor_init.py", line 34, in <module>
from . import _mklinit
ImportError: DLL load failed: The specified module could not be found.
I have installed all the modules, activated the env on Sublime too but still nothing.
Triend reinstalling everything but it's still nothing good, same errors.

Solved!
I had to downgrade the CUDA from 10.1 to 10.0 since apparently tensorflow only supports 10.0.

Related

Importing Keras for a CNN

I am getting a TypeErrir due to the added layer, BatchNormalization, not being the same as the class layer. I'm unsure why, I've tried to correctly import the layers, and have tried multiple different ways.
My imports are currently:
import copy
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from tqdm import tqdm
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import BatchNormalization,Dense, Conv2D, Flatten, Reshape
from tensorflow.keras.layers import Activation
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.layers import Input
I use the imports in the following section of code:
model = Sequential()
model.add(Input(shape=(9, 9, 1)))
model.add(Conv2D(64, kernel_size=(3, 3), activation='relu', padding='same'))
model.add(BatchNormalization)
model.add(Conv2D(64, kernel_size=(3, 3), activation='relu', padding='same'))
model.add(BatchNormalization)
model.add(Conv2D(128, kernel_size=(1, 1), activation='relu', padding='same'))
model.add(Flatten())
model.add(Dense(81 * 9))
model.add(Reshape((-1, 9)))
model.add(Activation('softmax'))
adam = Adam(lr=.001)
model.compile(loss='sparse_categorical_crossentropy', optimizer=adam)
model.fit(x_train, y_train, batch_size=32, epochs=2)
The error I am getting is:
File "**/train.py", line 24, in <module>
x_train, x_test, y_train, y_test = get_data('sudoku.csv')
File "**/data_preprocess.py", line 124, in get_data
model.add(BatchNormalization)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/tensorflow/python/training/tracking/base.py", line 457, in _method_wrapper
result = method(self, *args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/tensorflow/python/keras/engine/sequential.py", line 180, in add
raise TypeError('The added layer must be '
TypeError: The added layer must be an instance of class Layer. Found: <class 'tensorflow.python.keras.layers.normalization_v2.BatchNormalization'>
I also tried the following but am getting the same error.
Could the error be related to something else in the project? apart from the imports
You are almost there. Batchnorm is a class so you need to instantiate it by adding ()
model.add(Conv2D(64, kernel_size=(3, 3), activation='relu', padding='same'))
model.add(BatchNormalization())

How do I resolve the "TypeError: 'module' object is not callable" issue when trying to use the rmsprop optimizer?

Here is my code for the neural network I'm trying to get up:
from keras import layers
from keras import models
from keras import optimizers
from keras.preprocessing.image import ImageDataGenerator
train_dir = 'C:/Users/BaskaranBadr/Documents/Deep Learning/cats_and_dogs_small/train'
validation_dir = 'C:/Users/BaskaranBadr/Documents/Deep Learning/cats_and_dogs_small/validation'
model = models.Sequential()
model.add(layers.Conv2D(32, (3,3), activation='relu', input_shape = (150,150,3)))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Conv2D(64, (3,3), activation='relu', input_shape = (150,150,3)))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Conv2D(128, (3,3), activation='relu', input_shape = (150,150,3)))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Conv2D(128, (3,3), activation='relu', input_shape = (150,150,3)))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Flatten())
model.add(layers.Dense(512, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))
model.compile(loss='binarycrossentropy', optimizer=optimizers.rmsprop_v2(lr=0.0001), metrics = ['acc'])
The error I keep getting is this:
Traceback (most recent call last):
File "c:\Users\BaskaranBadr\Documents\Deep Learning\CatDogClassifier.py", line 24, in <module>
model.compile(loss='binarycrossentropy', optimizer=optimizers.rmsprop_v2(lr=0.0001), metrics = ['acc'])
TypeError: 'module' object is not callable
rmsprop_v2 is just an alias for rmsprop module inside optimizers package (see keras on GitHub).
You shouldn't use this alias. Just
from keras import optimizers
and then
opt = optimizers.RMSprop(learning_rate=0.0001)
model.compile(loss='binarycrossentropy', optimizer=opt, metrics = ['acc'])
I don't know if rmsprop_v2 is exist or not, or it is rmsprop of keras.optimizer_v2, you can check this link of keras.
If you want use RMSprop, you can follow this way:
import tensorflow as tf
optim = tf.keras.optimizers.RMSprop(lr=0.0001)
model.compile(loss='binarycrossentropy', optimizer=optim, metrics = ['acc'])
To access Keras in Tensorflow 1, use 'import keras'. Most older code and tutorials need their imports rewritten as 'from tensorflow.keras import X' when using Tensorflow 2.
Use:
from tensorflow.keras.optimizers import RMSprop

'BatchNormalization' is not defined

Trying to train a Robust CNN model which is defined as follows:
from keras.datasets import cifar10
from keras.utils import np_utils
from keras import metrics
from keras.models import Sequential
from keras.layers import Dense, Flatten, Conv2D, MaxPooling2D, LSTM, merge
from keras.layers import BatchNormalization
from keras import metrics
from keras.losses import categorical_crossentropy
from keras.optimizers import SGD
import pickle
import matplotlib.pyplot as plt
import numpy as np
from keras.preprocessing.image import ImageDataGenerator
from keras import layers
from keras.callbacks import EarlyStopping
def Robust_CNN():
model = Sequential()
model.add(Conv2D(256, (3, 3), activation='relu', padding='same', init='glorot_uniform', input_shape=(2,128,1)))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(1, 2), padding='valid', data_format=None))
model.add(layers.Dropout(.3))
model.add(Conv2D(128, (3, 3), activation='relu', init='glorot_uniform', padding='same'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(1, 2), padding='valid', data_format=None))
model.add(layers.Dropout(.3))
model.add(Conv2D(64, (3, 3), activation='relu', init='glorot_uniform', padding='same'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(1, 2), padding='valid', data_format=None))
model.add(layers.Dropout(.3))
model.add(Conv2D(64, (3, 3), activation='relu', init='glorot_uniform', padding='same'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(1, 2), padding='valid', data_format=None))
model.add(layers.Dropout(.3))
model.add(Flatten())
model.add(Dense(128, activation='relu', init='he_normal'))
model.add(BatchNormalization())
model.add(Dense(11, activation='softmax', init='he_normal'))
return model
However, when trying to do so I recieve a NameError that name 'BatchNormalization' is not defined. The complete error message is as follows:
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-11-8084d29438f8> in <module>
55 # >>>>>>>>>>>>>>>>>>>>> choose a model by un-commenting only one of the three <<<<<<<<<<<<<<<<<<<<<<<<<<<
56 #xx_shape = (2,128,1)
---> 57 models = Robust_CNN()
58 #models = CLDNN()
59 #models = resnet(xx_shape)
~\AppData\Local\Programs\Python\Python37\Scripts\FYP\Optimizing-Modulation-Classification-with-Deep-Learning-master\Optimizing-Modulation-Classification-with-Deep-Learning-master\Robust_CNN Model\model.py in Robust_CNN()
19 def Robust_CNN():
20
---> 21 model = Sequential()
22 model.add(Conv2D(256, (3, 3), activation='relu', padding='same', init='glorot_uniform', input_shape=(2,128,1)))
23 model.add(BatchNormalization())
NameError: name 'BatchNormalization' is not defined
Can't seem to figure out why this is even when I've already imported BatchNormalization.
First import BatchNormalization from tensorflow.keras.layers , then run your code
from tensorflow.keras.layers import BatchNormalization
Add this to your code-
from tensorflow.keras.layers import BatchNormalization
# import BatchNormalization
from keras.layers.normalization import BatchNormalization

TypeError: The added layer must be an instance of class Layer. Found: <keras.layers.core.Dropout object at 0x000001622999A5F8>

Import libraries and models,
from __future__ import print_function
import keras
from keras.datasets import mnist
from tensorflow.keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import MaxPooling2D
#from tensorflow.keras.layers import backend as k
batch_size = 128
num_classes = 10
epochs = 12
Below the written code,
model = Sequential()
model.add(Conv2D(32, kernel_size=(3,3), strides=(1,1), activation="relu", input_shape=(28, 28, 1) ))
model.add(Conv2D(32, kernel_size=(3,3), strides=(1,1), activation="relu"))
model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2) ))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
Below the type error, which I badly faced and i can't make the solution,
TypeError Traceback (most recent call last)
<ipython-input-6-6c99a01e13d4> in <module>
7 model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2) ))
8
----> 9 model.add(Dropout(0.5))
10 model.add(Flatten())
TypeError: The added layer must be an instance of class Layer. Found: <keras.layers.core.Dropout object at 0x000001622999A5F8>
Now, How should i solve this type of error?
Need Help,
Use Keras or tensorflow.keras, don't use both of them.
from __future__ import print_function
from tensorflow import keras
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Flatten
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import MaxPooling2D
from tensorflow.keras import backend as k
batch_size = 128
num_classes = 10
epochs = 12
model = Sequential()
model.add(Conv2D(32, kernel_size=(3,3), strides=(1,1), activation="relu", input_shape=(28, 28, 1) ))
model.add(Conv2D(32, kernel_size=(3,3), strides=(1,1), activation="relu"))
model.add(MaxPooling2D(pool_size=(2,2), strides=(2,2) ))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
The problem you have created your model using tensorflow.keras instance and you are trying to add layers of Keras instance.
Tensorflow has its own Keras version. So use only one.
Your code runs after fixing your import statements.
Code:
from __future__ import print_function
from tensorflow import keras
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Flatten
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import MaxPooling2D
#from tensorflow.keras.layers import backend as k

Keras | TypeError: __init__() missing 1 required positional argument: 'nb_col'

I am currently trying to implement this tutorial code into my own convnet.py but I get an error. Tutorial
This is the full error:
Traceback (most recent call last):
File "convnet.py", line 6, in <module>
model.add(Conv2D(32, (3, 3), input_shape=(3, 150, 150)))
TypeError: __init__() missing 1 required positional argument: 'nb_col'
Here are the first 10 lines on which the program goes wrong:
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(3, 150, 150)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
The code is located in the file convnet.py and I run the file like this:
python convnet.py
You are probably using an old version of Keras that had the following signature:
Conv2D(self, nb_filter, nb_row, nb_col, ...)
With this old version, you would define the conv layer as:
model.add(Conv2D(32, 3, 3, input_shape=(3, 150, 150)))
You can check the version you are using with:
import keras
print(keras.__version__)
I suggest that you update your Keras.

Categories