AttributeError: 'Tensor' object has no attribute '_keras_shape' - python

I'm trying to run code below to generate a JSON file and use it to built a t-SNE with a set of images. However my experience with Keras and machine learning is limited and I'm unable to run code below and getting error: AttributeError: 'Tensor' object has no attribute '_keras_shape'
import argparse
import sys
import numpy as np
import json
import os
from os.path import isfile, join
import keras
from keras.preprocessing import image
from keras.applications.imagenet_utils import decode_predictions, preprocess_input
from keras.models import Model
from sklearn.decomposition import PCA
from sklearn.manifold import TSNE
from scipy.spatial import distance
def process_arguments(args):
parser = argparse.ArgumentParser(description='tSNE on audio')
parser.add_argument('--images_path', action='store', help='path to directory of images')
parser.add_argument('--output_path', action='store', help='path to where to put output json file')
parser.add_argument('--num_dimensions', action='store', default=2, help='dimensionality of t-SNE points (default 2)')
parser.add_argument('--perplexity', action='store', default=30, help='perplexity of t-SNE (default 30)')
parser.add_argument('--learning_rate', action='store', default=150, help='learning rate of t-SNE (default 150)')
params = vars(parser.parse_args(args))
return params
def get_image(path, input_shape):
img = image.load_img(path, target_size=input_shape)
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
return x
def find_candidate_images(images_path):
"""
Finds all candidate images in the given folder and its sub-folders.
Returns:
images: a list of absolute paths to the discovered images.
"""
images = []
for root, dirs, files in os.walk(images_path):
for name in files:
file_path = os.path.abspath(os.path.join(root, name))
if ((os.path.splitext(name)[1]).lower() in ['.jpg','.png','.jpeg']):
images.append(file_path)
return images
def analyze_images(images_path):
# make feature_extractor
model = keras.applications.VGG16(weights='imagenet', include_top=True)
feat_extractor = Model(inputs=model.input, outputs=model.get_layer("fc2").output)
input_shape = model.input_shape[1:3]
# get images
candidate_images = find_candidate_images(images_path)
# analyze images and grab activations
activations = []
images = []
for idx,image_path in enumerate(candidate_images):
file_path = join(images_path,image_path)
img = get_image(file_path, input_shape);
if img is not None:
print("getting activations for %s %d/%d" % (image_path,idx,len(candidate_images)))
acts = feat_extractor.predict(img)[0]
activations.append(acts)
images.append(image_path)
# run PCA firt
print("Running PCA on %d images..." % len(activations))
features = np.array(activations)
pca = PCA(n_components=300)
pca.fit(features)
pca_features = pca.transform(features)
return images, pca_features
def run_tsne(images_path, output_path, tsne_dimensions, tsne_perplexity, tsne_learning_rate):
images, pca_features = analyze_images(images_path)
print("Running t-SNE on %d images..." % len(images))
X = np.array(pca_features)
tsne = TSNE(n_components=tsne_dimensions, learning_rate=tsne_learning_rate, perplexity=tsne_perplexity, verbose=2).fit_transform(X)
# save data to json
data = []
for i,f in enumerate(images):
point = [float((tsne[i,k] - np.min(tsne[:,k]))/(np.max(tsne[:,k]) - np.min(tsne[:,k]))) for k in range(tsne_dimensions) ]
data.append({"path":os.path.abspath(join(images_path,images[i])), "point":point})
with open(output_path, 'w') as outfile:
json.dump(data, outfile)
if __name__ == '__main__':
params = process_arguments(sys.argv[1:])
images_path = params['images_path']
output_path = params['output_path']
tsne_dimensions = int(params['num_dimensions'])
tsne_perplexity = int(params['perplexity'])
tsne_learning_rate = int(params['learning_rate'])
run_tsne(images_path, output_path, tsne_dimensions, tsne_perplexity, tsne_learning_rate)
print("finished saving %s" % output_path)
from: https://github.com/ml4a/ml4a-ofx/blob/master/scripts/tSNE-images.py
Here is what I'm getting:
Traceback (most recent call last):
File "tSNE-images.py", line 95, in <module>
run_tsne(images_path, output_path, tsne_dimensions, tsne_perplexity, tsne_learning_rate)
File "tSNE-images.py", line 75, in run_tsne
images, pca_features = analyze_images(images_path)
File "tSNE-images.py", line 50, in analyze_images
feat_extractor = Model(inputs=model.input, outputs=model.get_layer("fc2").output)
File "/Users/.../anaconda3/lib/python3.6/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "/Users/.../anaconda3/lib/python3.6/site-packages/keras/engine/network.py", line 91, in __init__
self._init_graph_network(*args, **kwargs)
File "/Users/.../anaconda3/lib/python3.6/site-packages/keras/engine/network.py", line 251, in _init_graph_network
input_shapes=[x._keras_shape for x in self.inputs],
File "/Users/.../anaconda3/lib/python3.6/site-packages/keras/engine/network.py", line 251, in <listcomp>
input_shapes=[x._keras_shape for x in self.inputs],
AttributeError: 'Tensor' object has no attribute '_keras_shape'
I found similar error in here:
`https://stackoverflow.com/questions/47616588/keras-throws-tensor-object-has-no-attribute-keras-shape-when-splitting-a`
However I can't seem to figure out how to go about updating code using Lambda. How can I solve this error?

I followed #user2300867 suggestion and updated tensorflow with:
pip3 install --upgrade tensorflow-gpu
and updated keras to 2.2.4
pip install Keras==2.2.4
I still got error:
TypeError: expected str, bytes or os.PathLike object, not NoneType
but this was easy to fix by simply editing the code for local paths

Related

ValueError: not enough values to unpack (expected 4, got 3) on torchvision

Trying to classify images using custom Pytorch model on Streamlit.
Now before prediction, the served image is first converted with torchvision, however, the following error pops up when I try to predict:
ValueError: not enough values to unpack (expected 4, got 3)
Traceback: File
"c:\users\pc\anaconda3\envs\tf-gpu2.5\lib\site-packages\streamlit\scriptrunner\script_runner.py",
line 557, in _run_script
exec(code, module.dict) File "app_v2.py", line 48, in
predict(image, model) File "app_v2.py", line 25, in predict
pred = model(x) File "c:\users\pc\anaconda3\envs\tf-gpu2.5\lib\site-packages\torch\nn\modules\module.py",
line 889, in _call_impl
result = self.forward(*input, **kwargs) File "c:\users\pc\anaconda3\envs\tf-gpu2.5\lib\site-packages\torch\autograd\grad_mode.py",
line 27, in decorate_context
return func(*args, **kwargs) File "C:\Users\PC/.cache\torch\hub\ultralytics_yolov5_master\models\common.py",
line 573, in forward
return self.model(imgs.to(p.device).type_as(p), augment, profile) # inference File "c:\users\pc\anaconda3\envs\tf-gpu2.5\lib\site-packages\torch\nn\modules\module.py",
line 889, in _call_impl
result = self.forward(*input, **kwargs) File "C:\Users\PC/.cache\torch\hub\ultralytics_yolov5_master\models\common.py",
line 443, in forward
b, ch, h, w = im.shape # batch, channel, height, width
code basically looks like:
## Serve Image
uploaded_file = st.file_uploader('Upload image...', type=['jpeg', 'jpg', 'png'])
if uploaded_file is not None:
## convert
image = Image.open(uploaded_file).convert('L')
predict(image, model)
def predict(image, model):
## convert
x = torchvision.transforms.ToTensor()(image)
pred = model(x)
pred = pred.detach().numpy()
Would appreciate the assistance.
Thanks
Your input image x has only three dimensions: channels (RGB), height and width.
PyTorch expects input images to have four dimensions: batch, channels, height and width.
You need to add a singleton "batch" dimension:
pred = model(x[None, ...])
So here's how I solved this problem, for anyone else, this was ultimately an Image Classification challenge using Pytorch's custom model on streamlit. So rigged the whole thing.
The following works.
import streamlit as st
from PIL import Image
import numpy as np
import torch
import io
## LOAD MODEL
run_model_path = './model_file.pt'
model = torch.hub.load('ultralytics/yolov5', 'custom', path=run_model_path)
model.eval()
## Collect
img_file_buffer = st.sidebar.file_uploader("Upload an Image", type=["jpg", "jpeg", "png"])
if img_file_buffer is not None:
image = np.array(Image.open(img_file_buffer))
## Predict Image
result = model(image)
## Convert Image format
output = io.BytesIO()
out_image = np.squeeze(result.render()) ## to numpy array
o_img = Image.fromarray(out_image) ## to PIL
o_img.save(output, format='JPEG')
result_img = output.getvalue() ## Back to JPEG
## Show Result
st.image(result_img)
So train (hard stuff) save in the same directory you run your app.
Phew.

AttributeError in loading training dataset when using Mask-RCNN

When using Mask-RCNN-TF2.7.0-keras2.8.0 for my test, I have some problems in just loading my training and validation dataset.
I provide my code here so you can better figure out what's wrong.
Basic setup
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # Avoid AVX Warning from using CPU
import sys
import random
import math
import re
import time
import numpy as np
import cv2
import matplotlib
import matplotlib.pyplot as plt
import h5py
from imgaug import augmenters as iaa
from imgaug import parameters as iap
from glob import glob
# Root directory of the project
ROOT_DIR = os.path.abspath('/Mask_RCNN2.0/')
# Import Mask RCNN
sys.path.append(ROOT_DIR) # To find local version of the library
from mrcnn.config import Config
from mrcnn import utils
import mrcnn.model as modellib
from mrcnn import visualize
from mrcnn.model import log
# Directory to save logs and trained model
MODEL_DIR = os.path.join(ROOT_DIR, "logs")
# Local path to trained weights file
COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")
# Download COCO trained weights from Releases if needed
if not os.path.exists(COCO_MODEL_PATH):
utils.download_trained_weights(COCO_MODEL_PATH)
import tensorflow as tf
config = tf.compat.v1.ConfigProto()
gpus = tf.config.experimental.list_physical_devices('GPU') # If there is any GPU available
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True) # Allocating GPU memory
# config.gpu_options.allow_growth=True
config.gpu_options.per_process_gpu_memory_fraction = 0.6 # Maximum consumption of GPU limit to 0.6
sess = tf.compat.v1.Session(config=config)
# for visualization
def get_ax(rows=1, cols=1, size=8):
"""Return a Matplotlib Axes array to be used in
all visualizations in the notebook. Provide a
central point to control graph sizes.
Change the default size attribute to control the size
of rendered images
"""
_, ax = plt.subplots(rows, cols, figsize=(size*cols, size*rows))
return ax
Configuration
classes = {1: 'trout'}
class Config(Config):
NAME = 'trout'
GPU_COUNT = 1
IMAGES_PER_GPU = 1
# BATCH_SIZE = IMAGES_PER_GPU * GPU_COUNT
NUM_CLASSES = len(classes) + 1 # background + number of classes
IMAGE_MIN_DIM = 128
IMAGE_MAX_DIM = 128
IMAGE_RESIZE_MODE = 'square'
# Use smaller anchors because our image and objects are small
RPN_ANCHOR_SCALES = (8, 16, 32, 64, 128) # Refer to the setup in train_shapes.ipynb
# Reduce training ROIs per image because the images are small and have
# few objects. Aim to allow ROI sampling to pick 33% positive ROIs.
TRAIN_ROIS_PER_IMAGE = 32
# Use a small epoch since the data is simple
STEPS_PER_EPOCH = 100
# use small validation steps since the epoch is small
VALIDATION_STEPS = 5
config = Config()
config.display()
Dataset
from multiprocessing import Lock, Value, Pool, cpu_count
import tqdm
def load_images(i, images, annotation_file, temp):
path = os.path.join(images[i], 'image')
img_path = os.path.join(temp, os.path.split(path)[0].replace('/', '-').replace(':', '') + '.png')
if os.path.exists(img_path):
height, width = cv2.imread(img_path).shape[:2]
else:
with h5py.File(annotation_file, 'r') as h5_file:
height, width = h5_file[path][:].shape[:2]
image = h5_file[path][:]
cv2.imwrite(img_path, image[:, :, ::-1])
lock.acquire()
count.value += 1
print('Reading images: {:.2f} %'.format(100 * count.value / len(images)),
sep=' ', end='\r' if count.value < len(images) else '\n', flush=True)
lock.release()
return (i, path, width, height)
def init_pool(l, c):
global lock, count
lock = l
count = c
class Dataset(utils.Dataset):
def load_images(self, annotation_file, classes, source, mode='full'):
self.classes = classes
self.annotation_file = annotation_file
self.temp = os.path.join(os.path.join(os.path.dirname(annotation_file), os.path.split(annotation_file)[-1] + 'temp'))
if not os.path.exists(self.temp):
os.mkdir(self.temp)
with h5py.File(self.annotation_file, 'r') as h5_file:
for c in self.classes:
self.add_class(source, c, self.classes[c])
self.images = h5_file['annotations'][:].astype(np.str)
np.random.seed(0)
image_idx = np.arange(self.images.size)
val_idx = np.random.choice(image_idx, image_idx.size // 6)
if mode == 'train':
self.images = self.images[np.invert(np.isin(image_idx, val_idx))]
elif mode == 'val':
self.images = self.images[np.isin(image_idx, val_idx)]
else:
print('Warning: set mode to "train" or "val", otherwise using full dataset')
l = Lock()
c = Value('d', 0)
if __name__ == "__main__":
with Pool(processes=cpu_count(), initializer=init_pool, initargs=(l, c)) as pool:
to_add = pool.starmap(load_images, list(zip(np.arange(len(self.images)),
[self.images] * len(self.images),
[self.annotation_file] * len(self.images),
[self.temp] * len(self.images))))
for (i, path, width, height) in to_add:
self.add_image(source, image_id=i, path=path, width=width, height=height)
pool.close()
pool.join()
def load_image(self, image_id):
info = self.image_info[image_id]
path = info['path']
img_path = os.path.join(self.temp, os.path.split(path)[0].replace('/', '-').replace(':', '') + '.png')
print(img_path)
image = cv2.imread(img_path)[:, :, ::-1]
return image
def load_mask(self, image_id):
info = self.image_info[image_id]
img_path = info['path']
path = os.path.split(img_path)[0]
with h5py.File(self.annotation_file, 'r') as h5_file:
mask = h5_file[os.path.join(path, 'mask')][:]
classes = h5_file[os.path.join(path, 'class_names')][:].astype(np.str)
use = np.array([idx for idx, name in enumerate(classes) for c in self.classes if name in self.classes[c].split(',')], dtype=np.int32)
class_ids = np.array([c for name in classes for c in self.classes if name in self.classes[c].split(',')], dtype=np.int32)
mask = mask[:, :, use]
non_empty = mask.sum(axis=(0, 1)) > 10
return mask[:, :, non_empty], class_ids[non_empty]
def prepare(self):
super().prepare()
print('{} images, classes: '.format(len(self.image_ids)), *['[{}: {}]'.format(idx, self.classes[idx]) for idx in self.classes])
augmentation = iaa.Sequential([
iaa.Fliplr(0.5),
iaa.Flipud(0.5),
iaa.Affine(rotate=iap.Choice([0,90,180,270])),
iaa.Affine(scale={'x': (0.8, 1.2), 'y': (0.8, 1.2)},
translate_percent={'x': (-0.2, 0.2), 'y': (-0.2, 0.2)},
rotate=(-45, 45))
], random_order=True)
Load dataset (where errors occur)
dataset_train = Dataset()
dataset_train.load_images(annotation_file='/Mask_RCNN2.0/trout/Dataset/314_0_out.h5',
classes=classes,
source='trout',
mode = 'train')
dataset_train.prepare()
The error info is as follow, and it seems to be circulating and can't stop even if I stop the kernel.
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Apps\Anaconda3\envs\CNNTest2\lib\multiprocessing\spawn.py", line 116, in spawn_main
exitcode = _main(fd, parent_sentinel)
File "C:\Apps\Anaconda3\envs\CNNTest2\lib\multiprocessing\spawn.py", line 125, in _main
prepare(preparation_data)
File "C:\Apps\Anaconda3\envs\CNNTest2\lib\multiprocessing\spawn.py", line 236, in prepare
_fixup_main_from_path(data['init_main_from_path'])
File "C:\Apps\Anaconda3\envs\CNNTest2\lib\multiprocessing\spawn.py", line 287, in _fixup_main_from_path
main_content = runpy.run_path(main_path,
File "C:\Apps\Anaconda3\envs\CNNTest2\lib\runpy.py", line 265, in run_path
return _run_module_code(code, init_globals, run_name,
File "C:\Apps\Anaconda3\envs\CNNTest2\lib\runpy.py", line 97, in _run_module_code
_run_code(code, mod_globals, init_globals,
File "C:\Apps\Anaconda3\envs\CNNTest2\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "C:\Mask_RCNN2.0\trout\FishSeg_0516.py", line 271, in <module>
model.train(dataset_train, dataset_val,
File "C:\Apps\Anaconda3\envs\CNNTest2\lib\site-packages\mrcnn\model.py", line 2354, in train
self.compile(learning_rate, self.config.LEARNING_MOMENTUM)
File "C:\Apps\Anaconda3\envs\CNNTest2\lib\site-packages\mrcnn\model.py", line 2201, in compile
self.keras_model.add_metric(loss, name=name, aggregation='mean')
File "C:\Apps\Anaconda3\envs\CNNTest2\lib\site-packages\keras\engine\base_layer_v1.py", line 1132, in add_metric
self._graph_network_add_metric(value, aggregation, name)
File "C:\Apps\Anaconda3\envs\CNNTest2\lib\site-packages\keras\engine\functional.py", line 914, in _graph_network_add_metric
add_metric_layer(value)
File "C:\Apps\Anaconda3\envs\CNNTest2\lib\site-packages\keras\engine\base_layer_v1.py", line 765, in __call__
outputs = call_fn(cast_inputs, *args, **kwargs)
File "C:\Apps\Anaconda3\envs\CNNTest2\lib\site-packages\keras\engine\base_layer.py", line 3359, in call
self.add_metric(inputs, aggregation=self.aggregation, name=self.metric_name)
File "C:\Apps\Anaconda3\envs\CNNTest2\lib\site-packages\keras\engine\base_layer_v1.py", line 1113, in add_metric
self._symbolic_add_metric(value, aggregation, name)
File "C:\Apps\Anaconda3\envs\CNNTest2\lib\site-packages\keras\engine\base_layer_v1.py", line 1887, in _symbolic_add_metric
metric_obj, result_tensor = base_layer_utils.create_mean_metric(
File "C:\Apps\Anaconda3\envs\CNNTest2\lib\site-packages\keras\engine\base_layer_utils.py", line 35, in create_mean_metric
return metric_obj, metric_obj(value)
File "C:\Apps\Anaconda3\envs\CNNTest2\lib\site-packages\keras\metrics.py", line 237, in __call__
return distributed_training_utils.call_replica_local_fn(
File "C:\Apps\Anaconda3\envs\CNNTest2\lib\site-packages\keras\distribute\distributed_training_utils.py", line 60, in call_replica_local_fn
return fn(*args, **kwargs)
File "C:\Apps\Anaconda3\envs\CNNTest2\lib\site-packages\keras\metrics.py", line 223, in replica_local_fn
result_t = self.result() # pylint: disable=not-callable
File "C:\Apps\Anaconda3\envs\CNNTest2\lib\site-packages\keras\utils\metrics_utils.py", line 124, in decorated
tf.__internal__.distribute.strategy_supports_no_merge_call()):
AttributeError: module 'tensorflow.compat.v2.__internal__.distribute' has no attribute 'strategy_supports_no_merge_call'
My computer setup:
GPU: NVIDIA Quadro P620, 2GB
CUDA: 11.2
cuDNN: 8.1
tensorflow 2.8.0
Keras 2.7.0
Windows 10
Loading dataset only takes a few second when I run my code on Google Colab (Ubuntun 18.04, run perfectly), but it takes a long time on local machine with the error message AttributeError: module 'tensorflow.compat.v2.__internal__.distribute' has no attribute 'strategy_supports_no_merge_call, and both CPU and GPU are almost out of memory.
I wonder if there is something different between Ubuntun/Linux system and windows 10 that I haven't notice? I have previously tested tensorflow 1.x on windows 10, and similar problem occurs. This really troubles me for a long time. Could anyone please help me?
Thanks in advance for your kindness.
Best,
Erin

Why streamlit is throwing out "ValueError: No model found in config file"?

`ValueError: No model found in config file.
Traceback:
File "c:\users\makje\anaconda3\envs\tensorflow\lib\site-packages\streamlit\script_runner.py", line 338, in _run_script
exec(code, module.__dict__)
File "C:\Users\makje\Documents\CODE\Python\A.I.(intern)\major.py", line 8, in <module>
model = tf.keras.models.load_model("C:\\Users\\makje\\Documents\\CODE\\Python\\A.I(intern)\\malay_model.hdf5")
File "c:\users\makje\anaconda3\envs\tensorflow\lib\site- packages\tensorflow\python\keras\saving\save.py", line 182, in load_model
return hdf5_format.load_model_from_hdf5(filepath, custom_objects, compile)
File "c:\users\makje\anaconda3\envs\tensorflow\lib\site-packages\tensorflow\python\keras\saving\hdf5_format.py", line 175, in load_model_from_hdf5
raise ValueError('No model found in config file.')`
These are the things that need to know before going to any conclusion.
I've created a model inside a jupyter notebook.
It's 100% working(accurate prediction).
Model object is saved locally inside my 'C' drive.
I've created a streamlit web app inside a jupyter notebook
Given a streamlit code below\
%%writefile dvc.py
import streamlit as st
import tensorflow as tf
import numpy as np
import streamlit as st
from PIL import Image
import requests
from io import BytesIO
st.set_option('deprecation.showfileUploaderEncoding', False)
st.title("Dogs and Cats Classification")
st.text("Provide an Image for image classification")
#st.cache(allow_output_mutation=True)
def load_model():
model = tf.keras.models.load_model('C:\\Users\\makje\\Documents\\CODE\\Python\\A.I(intern)\\model.h5')
return model
with st.spinner('Loading Model Into Memory....'):
model = load_model()
def scale(image):
image = tf.cast(image, tf.float32)
image /= 255.0
return tf.image.resize(image,[128,128])
def decode_img(image):
img = tf.image.decode_jpeg(image, channels=3)
img = scale(img)
return np.expand_dims(img, axis=0)
path = st.text_input('Enter Image URL to classify...','https://www.thesprucepets.com/thmb/fkocatcypSHCKeezdfPdGXOi3FU=/1333x1000/smart/filters:no_upscale()/top-friendliest-dog-breeds-4691511_hero-5c6a918dcf56409c888d78b0fac82d18.jpg')
if path is not None:
content = requests.get(path).content
st.write("Predicted Class")
with st.spinner('Classifying....'):
label = np.argmax(model.predict(decode_img(content)),axis=1)
st.write(classes[label[0]])
st.write("")
image = Image.open(BytesIO(contenttent))
st.image(image, caption='Classifying Dog/Cat Image', use_column_width=True)

TypeError('No suitable SharedVariable constructor could be found.') Using lasagne and theano

Hi i'm currently learning coding with python and have been following a tutorial series which has helped me make the code i will show below. Apologies for it being so long but I cannot pinpoint the line of code which is causing this error. I have removed a lot of the commenting to reduce the amount of code posted.
import numpy as np
import urllib.request
import os
import gzip
import lasagne
import theano
import theano.tensor as T
def load_dataset():
def download(filename, source="http://yann.lecun.com/exdb/mnist/"):
print("downloading:", filename)
urllib.request.urlretrieve(source+filename, filename)
def load_mnist_images(filename):
if not os.path.exists(filename):
download(filename)
with gzip.open(filename, "rb") as f:
data = np.frombuffer(f.read(), np.uint8, offset= 16)
data = data.reshape(-1, 1, 28, 28)
return data / np.float32(256)
def load_mnist_labels(filename):
if not os.path.exists(filename):
download(filename)
with gzip.open(filename, "rb") as f:
data = np.frombuffer(f.read(), np.uint8, offset= 8)
return data
x_train = load_mnist_images("train-images-idx3-ubyte.gz")
y_train = load_mnist_labels("train-labels-idx1-ubyte.gz")
x_test = load_mnist_images("t10k-images-idx3-ubyte.gz")
y_test = load_mnist_labels("t10k-labels-idx1-ubyte.gz")
return x_train, y_train, x_test, y_test
x_train, y_train, x_test, y_test = load_dataset()
###### creating the handwriting digit recognition code ######
def build_nn(input_var = None):
l_in = lasagne.layers.InputLayer(shape=(None,1,28,28), input_var=input_var)
l_in_drop = lasagne.layers.DropoutLayer(l_in, p=0.2)
l_hid1 = lasagne.layers.DenseLayer(l_in_drop, num_units= 800,
nonlinearity= lasagne.nonlinearities.rectify,
W= lasagne.init.GlorotUniform())
l_hid1_drop = lasagne.layers.DropoutLayer(l_hid1, p=0.5)
l_hid2 = lasagne.layers.DenseLayer(l_hid1_drop, num_units= 800,
nonlinearity= lasagne.nonlinearities.rectify,
W= lasagne.init.GlorotUniform())
l_hid2_drop = lasagne.layers.DropoutLayer(l_hid2, p=0.5)
l_out = lasagne.layers.DenseLayer(l_hid2_drop, num_units=10,
nonlinearity= lasagne.nonlinearities.softmax)
return l_out
input_var = T.tensor4("inputs") # an empty 4d array
target_var = T.ivector("targets") # an empty 1d int array to represent the labels
network = build_nn(input_var) # call the func that initializes the neural network
prediction = lasagne.layers.get_output(network)
loss = lasagne.objectives.categorical_crossentropy(prediction, target_var)
loss = loss.mean()
params = lasagne.layers.get_all_params(network, trainable=True)
updates = lasagne.updates.nesterov_momentum(loss, params, learning_rate=0.01, momentum=0.9)
train_fn = theano.function([input_var, target_var], loss, updates= updates)
num_training_steps = 10
for step in range(num_training_steps):
train_err = train_fn(x_train, y_train)
print("current training step is " + str(step))
The error that's stopping this code is this:
Traceback (most recent call last):
File "C:\Users\Admin\.vscode\Practice codes\machine learning\deep learning\deep learning.py", line 125, in <module>
network = build_nn(input_var) # call the func that initializes the neural network
File "C:\Users\Admin\.vscode\Practice codes\machine learning\deep learning\deep learning.py", line 95, in build_nn
l_hid1 = lasagne.layers.DenseLayer(l_in_drop, num_units= 800,
File "C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\lasagne\layers\dense.py", line 103, in __init__
self.W = self.add_param(W, (num_inputs, num_units), name="W")
File "C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\lasagne\layers\base.py", line 234, in add_param
param = utils.create_param(spec, shape, name)
File "C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\lasagne\utils.py", line 393, in create_param
spec = theano.shared(spec, broadcastable=bcast)
File "C:\Users\Admin\AppData\Roaming\Python\Python38\site-packages\theano\compile\sharedvalue.py", line 284, in shared
raise TypeError('No suitable SharedVariable constructor could be found.'
TypeError: No suitable SharedVariable constructor could be found. Are you sure all kwargs are supported? We do not support the parameter dtype or type. value="[[ 0.04638761 -0.02959769 0.02330909 ... 0.01545383 0.04763002
0.05265676]
[ 0.02095251 -0.05393376 -0.04289599 ... -0.02409102 0.02824548
-0.00327342]
[ 0.02908951 -0.02853872 -0.05450716 ... -0.02296509 0.02495853
0.02486875]
...
[-0.03704383 0.0286258 0.01158947 ... -0.02583007 -0.04925423
-0.0470493 ]
[ 0.03230407 -0.00246115 -0.05074456 ... 0.00299953 0.01883504
0.01312843]
[-0.05762409 -0.05119916 -0.02820581 ... -0.05675326 0.00458562
0.04403118]]". parameters="{'broadcastable': (False, False)}"
If it helps I'm using python 3.8 - lasagne 0.2.dev1 - theano 1.0.5.
Any help would be greatly appreciated, any questions feel free to ask.
Thanks in advance

buiding a network using lasagne and theano

I am trying to build a network but I am getting the following error
this is the building and training code
The network was built by this following video code
The data I created using this repository githup repository
The train contains 24 class of images and the test contains 24 class of labels
import os
import numpy as np
import lasagne
import theano
import theano.tensor as T
def load_mnist_images(filename):
with gzip.open(filename,'rb') as f :
data = np.frombuffer(f.read(),np.uint8,offset=16)
data = data.reshape(-1,1,28,28)
print(type(data))
return data/np.float32(256)
def load_mnist_labels(filename):
with gzip.open(filename,'rb')as f:
data=np.frombuffer(f.read(),np.uint8,offset=8)
return data
def load_dataset():
x_train = load_mnist_images('train-images-idx3-ubyte.gz')
y_train = load_mnist_labels('train-labels-idx1-ubyte.gz')
x_test = load_mnist_images('test-images-idx3-ubyte.gz')
y_test = load_mnist_labels('test-labels-idx1-ubyte.gz')
return x_train,y_train,x_test,y_test
def build_nn(input_var=None):
l_in = lasagne.layers.InputLayer(shape=(None,1,28,28),input_var=input_var)
l_in_drop = lasagne.layers.DropoutLayer(l_in,p=0.2)
l_hid1 = lasagne.layers.DenseLayer(l_in_drop,num_units=800,
nonlinearity=lasagne.nonlinearities.rectify,
W=lasagne.init.GlorotUniform())
l_hid1_drop = lasagne.layers.DropoutLayer(l_hid1,p=0.5)
l_hid2 = lasagne.layers.DenseLayer(l_hid1_drop, num_units=800,
nonlinearity=lasagne.nonlinearities.rectify,
W=lasagne.init.GlorotUniform())
l_hid2_drop = lasagne.layers.DropoutLayer(l_hid2, p=0.5)
l_out = lasagne.layers.DenseLayer(l_hid2_drop,num_units=24 ,
nonlinearity= lasagne.nonlinearities.softmax)
return l_out
if __name__ == "__main__":
x_train,y_train,x_test,y_test = load_dataset()
input_var = T.tensor4('inputs')
target_var = T.tensor4('targets')
print(target_var)
network = build_nn(input_var)
prediction = lasagne.layers.get_output(network)
loss = lasagne.objectives.categorical_crossentropy(prediction,target_var)
loss = loss.mean()
params = lasagne.layers.get_all_params(network, trainable=True)
updates = lasagne.updates.nesterov_momentum(loss,params,learning_rate=0.01 , momentum=0.9)
train_fn = theano.function([input_var,target_var],loss , updates=updates)
num_training_steps = 10
for steps in range(num_training_steps):
train_err = train_fn(x_train,y_train)
print("current step is " + str(steps))
and this is the error i get
Traceback (most recent call last):
File "/home/hassan/JPG-PNG-to-MNIST-NN-Format/mnist_test.py", line 64, in <module>
loss = lasagne.objectives.categorical_crossentropy(prediction,target_var)
File "/home/hassan/anaconda3/envs/object-detection/lib/python3.7/site-packages/lasagne/objectives.py", line 181, in categorical_crossentropy
return theano.tensor.nnet.categorical_crossentropy(predictions, targets)
File "/home/hassan/anaconda3/envs/object-detection/lib/python3.7/site-packages/theano/tensor/nnet/nnet.py", line 2101, in categorical_crossentropy
raise TypeError('rank mismatch between coding and true distributions')
TypeError: rank mismatch between coding and true distributions
I solved it there was a mistake in writing te code
instead of writing this
target_var = T.ivector('targets')
I wrote this
target_var = T.tensor4('targets')

Categories

Resources