train image classification models with colab - tensorflow

I follow the template and change the link , but it doesn't work
https://colab.research.google.com/github/tensorflow/tensorflow/blob/master/tensorflow/lite/g3doc/tutorials/model_maker_image_classification.ipynb#scrollTo=3jz5x0JoskPv
This is my datasets
https://firebasestorage.googleapis.com/v0/b/lol-fypproject.appspot.com/o/lol.tgz?alt=media&token=d07b81bd-442f-4ebe-920e-3772598fbb20
original code
image_path = tf.keras.utils.get_file(
'flower_photos.tgz',
'https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz',
extract=True)
image_path = os.path.join(os.path.dirname(image_path), 'flower_photos')
I changed in that
image_path = tf.keras.utils.get_file(
'lol.tgz',
'https://firebasestorage.googleapis.com/v0/b/lol-fypproject.appspot.com/o/lol.tgz?alt=media&token=d07b81bd-442f-4ebe-920e-3772598fbb20',
extract=True)
image_path = os.path.join(os.path.dirname(image_path), 'lol')
the line wrong and error message is showed
data = ImageClassifierDataLoader.from_folder(image_path)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-15-a5e7646aca55> in <module>()
----> 1 data = ImageClassifierDataLoader.from_folder(image_path)
2 train_data, test_data = data.split(0.9)
/usr/local/lib/python3.7/dist-
packages/tensorflow_examples/lite/model_maker/core/data_util/image_dataloader.py
in
from_folder(cls, filename, shuffle)
69 all_image_size = len(all_image_paths)
70 if all_image_size == 0:
---> 71 raise ValueError('Image size is zero')
72
73 if shuffle:
ValueError: Image size is zero

I have find the problem
the path of the zip file is not the right structure as the sample

Related

AttributeError: 'ArrayView' object has no attribute 'A1'

I have to import a processed h5ad file, but it seems that X has been passed as a numpy array instead of a numpy matrix. See below:
# Read the data
data_path = "/home/bbb5130/snOMICS/maria/msrna.h5ad"
adata = sn.pp.read_h5ad(data_path, pr_process="Yes")
adata
But the output was:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In [15], line 3
1 # Read the data
2 data_path = "/home/bbb5130/snOMICS/maria/msrna.h5ad"
----> 3 adata = sn.pp.read_h5ad(data_path, pr_process="Yes")
4 adata
File ~/miniconda3/envs/snOMICS/lib/python3.9/site-packages/scanet/preprocessing.py:54, in Preprocessing.read_h5ad(cls, filename, pr_process)
51 return sc.read_h5ad(filename)
52 else:
53 # initial preprocessing as it is required later
---> 54 return cls._intial(adata)
File ~/miniconda3/envs/snOMICS/lib/python3.9/site-packages/scanet/preprocessing.py:35, in Preprocessing._intial(adata)
33 adata.var['mt'] = adata.var_names.str.startswith('MT-')
34 mito_genes = adata.var_names.str.startswith('MT-')
---> 35 adata.obs['percent_mito'] = np.sum(adata[:, mito_genes].X, axis=1).A1 / np.sum(adata.X, axis=1).A1
36 sc.pp.calculate_qc_metrics(adata, qc_vars=['mt'], percent_top=None, inplace=True)
37 sc.pp.filter_cells(adata, min_genes=0)
AttributeError: 'ArrayView' object has no attribute 'A1'
Is there anyway I can change the format, so the file can be read?
Thanks in advance.

Problem with 'Unknown image file format' error for GCS image in Tensorflow style transfer demo

I'm wanting to use my own images for this Tensorflow Style Transfer demo, which I've copied to my own Colab notebook.
[https://www.tensorflow.org/hub/tutorials/tf2_arbitrary_image_stylization][1]
I have images stored in a GCS bucket but have been getting image format errors. To test this, I took one of the images from the Tensorflow demo, downloaded it and put it in my GCS bucket, added the link to the "Let's try it on more images" section of my demo code, and am getting the same file format error message I was previously getting with my own images.
Here's where I've inserted the GCS version of the image:
content_urls = dict(
tueblingen02='https://storage.cloud.google.com/01_bucket-02/Tuebingen_Neckarfront-vox.jpeg',
sea_turtle='https://upload.wikimedia.org/wikipedia/commons/d/d7/Green_Sea_Turtle_grazing_seagrass.jpg',
tuebingen='https://upload.wikimedia.org/wikipedia/commons/0/00/Tuebingen_Neckarfront.jpg',
grace_hopper='https://storage.googleapis.com/download.tensorflow.org/example_images/grace_hopper.jpg',
)
style_urls = dict(
kanagawa_great_wave='https://upload.wikimedia.org/wikipedia/commons/0/0a/The_Great_Wave_off_Kanagawa.jpg',
kandinsky_composition_7='https://upload.wikimedia.org/wikipedia/commons/b/b4/Vassily_Kandinsky%2C_1913_-_Composition_7.jpg',
etc ...
The resulting error message:
InvalidArgumentError: Unknown image file format. One of JPEG, PNG, GIF, BMP required. [Op:DecodeImage]
Full message:
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
<ipython-input-16-3ded16359898> in <module>()
26 content_image_size = 384
27 style_image_size = 256
---> 28 content_images = {k: load_image(v, (content_image_size, content_image_size)) for k, v in content_urls.items()}
29 style_images = {k: load_image(v, (style_image_size, style_image_size)) for k, v in style_urls.items()}
30 style_images = {k: tf.nn.avg_pool(style_image, ksize=[3,3], strides=[1,1], padding='SAME') for k, style_image in style_images.items()}
3 frames
<ipython-input-16-3ded16359898> in <dictcomp>(.0)
26 content_image_size = 384
27 style_image_size = 256
---> 28 content_images = {k: load_image(v, (content_image_size, content_image_size)) for k, v in content_urls.items()}
29 style_images = {k: load_image(v, (style_image_size, style_image_size)) for k, v in style_urls.items()}
30 style_images = {k: tf.nn.avg_pool(style_image, ksize=[3,3], strides=[1,1], padding='SAME') for k, style_image in style_images.items()}
<ipython-input-2-1485a3082999> in load_image(image_url, image_size, preserve_aspect_ratio)
19 img = tf.io.decode_image(
20 tf.io.read_file(image_path),
---> 21 channels=3, dtype=tf.float32)[tf.newaxis, ...]
22 img = crop_center(img)
23 img = tf.image.resize(img, image_size, preserve_aspect_ratio=True)
/usr/local/lib/python3.7/dist-packages/tensorflow/python/util/traceback_utils.py in error_handler(*args, **kwargs)
151 except Exception as e:
152 filtered_tb = _process_traceback_frames(e.__traceback__)
--> 153 raise e.with_traceback(filtered_tb) from None
154 finally:
155 del filtered_tb
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/ops.py in raise_from_not_ok_status(e, name)
7184 def raise_from_not_ok_status(e, name):
7185 e.message += (" name: " + name if name is not None else "")
-> 7186 raise core._status_to_exception(e) from None # pylint: disable=protected-access
7187
7188
InvalidArgumentError: Unknown image file format. One of JPEG, PNG, GIF, BMP required. [Op:DecodeImage]
So that's confusing me because the image works fine when it's hosted elsewhere, leading me to believe it's not an image format issue, but something else.
I'd greatly appreciate any input or suggestions on what might be happening here.
thx

CNN Cannot Identify Image File

I have made a simple CNN to recognize three types of fish. I am trying to use CNN to classify the image that was not included in training or validation sets. The image is grunts-saltwater.jpg and is on Gdrive. Here is the code for predicting on existing CNN model:
grunts_url = "https://drive.google.com/file/d/1zuA6T_0a9mOUvNWHQ1OACPLaZMCtbIZd/view?usp=sharing"
grunts_path = tf.keras.utils.get_file('grunts-saltwater', origin=grunts_url)
img = keras.preprocessing.image.load_img(
grunts_path, target_size=(img_height, img_width))
img_array = keras.preprocessing.image.img_to_array(img)
img_array = tf.expand_dims(img_array, 0) # Create a batch
predictions = model.predict(img_array)
score = tf.nn.softmax(predictions[0])
print(
"This image most likely belongs to {} with a {:.2f} percent confidence."
.format(class_names[np.argmax(score)], 100 * np.max(score))
)
However, I get the following error:
Downloading data from https://drive.google.com/file/d/1zuA6T_0a9mOUvNWHQ1OACPLaZMCtbIZd/view?usp=sharing
8192/Unknown - 0s 0us/step
---------------------------------------------------------------------------
UnidentifiedImageError Traceback (most recent call last)
<ipython-input-217-d031443047e1> in <module>()
3
4 img = keras.preprocessing.image.load_img(
----> 5 grunts_path, target_size=(img_height, img_width))
6
7 img_array = keras.preprocessing.image.img_to_array(img)
2 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/preprocessing/image.py in load_img(path, grayscale, color_mode, target_size, interpolation)
299 """
300 return image.load_img(path, grayscale=grayscale, color_mode=color_mode,
--> 301 target_size=target_size, interpolation=interpolation)
302
303
/usr/local/lib/python3.6/dist-packages/keras_preprocessing/image/utils.py in load_img(path, grayscale, color_mode, target_size, interpolation)
112 'The use of `load_img` requires PIL.')
113 with open(path, 'rb') as f:
--> 114 img = pil_image.open(io.BytesIO(f.read()))
115 if color_mode == 'grayscale':
116 # if image is not already an 8-bit, 16-bit or 32-bit grayscale image
/usr/local/lib/python3.6/dist-packages/PIL/Image.py in open(fp, mode)
2860 warnings.warn(message)
2861 raise UnidentifiedImageError(
-> 2862 "cannot identify image file %r" % (filename if filename else fp)
2863 )
2864
UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x7f9002c637d8>
Can you help with the issue, please? Thanks.
It is because the google drive link of the image is now a downloadable line rather it is a view link. If it is shared then you can convert it into a downloadable link such that http clients can download them.
To covert to downloadable line
Your shared image ID is 1zuA6T_0a9mOUvNWHQ1OACPLaZMCtbIZd so use the link "https://docs.google.com/uc?export=download&id=1zuA6T_0a9mOUvNWHQ1OACPLaZMCtbIZd"
Fixed code:
grunts_url = "https://docs.google.com/uc?export=download&id=1zuA6T_0a9mOUvNWHQ1OACPLaZMCtbIZd"
grunts_path = tf.keras.utils.get_file('grunts-saltwater', origin=grunts_url)
img = keras.preprocessing.image.load_img(grunts_path, target_size=(100, 100))
I managed to resolve the issue. I do not think I was referring to source URL correctly. Here is an example that worked.
gruntfish_url = "https://upload.wikimedia.org/wikipedia/commons/5/54/Blue_Stripe_Grunt._Haemulon_sciurus.jpg"
gruntfish_path = tf.keras.utils.get_file('Grunt.', origin=gruntfish_url)

Displaying Image in python

I have created a function which adds all my images to a list.
The function is as follows:
def load_data(train_path,test_path):
X_train=[]
X_test=[]
for i in os.listdir(train_path):
X_train.append(i)
for j in os.listdir(test_path):
X_test.append(j)
return X_train,X_test
When I try to display Image using indexing X_train[10] I get a file not found Error.
img=mpimg.imread(X_train[10])
imgplot = plt.imshow(img)
plt.show()
The error is as Followed:
FileNotFoundError Traceback (most recent call last)
<ipython-input-7-869e21232029> in <module>()
----> 1 img=mpimg.imread(X_train[10])
2 imgplot = plt.imshow(img)
3 plt.show()
/Users/ViditShah/anaconda/envs/dl/lib/python3.6/site-packages/matplotlib/image.py in imread(fname, format)
1295 return handler(fd)
1296 else:
-> 1297 with open(fname, 'rb') as fd:
1298 return handler(fd)
1299 else:
FileNotFoundError: [Errno 2] No such file or directory: 'scan_0001001.png'
listdir() only returns the file name, not the full path.
You need to store the full file path in your list
X_train.append(os.path.join(train_path, i))

tf.contrib.learn yields error message "module has no attribute 'learn' "

Here is a snippet of my code taken directly from the tf.contrib.learn tutorial on tensorflow.org:
# Load Data Sets
training_set = tf.contrib.learn.datasets.base.load_csv_with_header(
filename = IRIS_TRAINING,
target_dtype = np.int,
features_dtype = np.float32)
Here is the error message:
AttributeError Traceback (most recent call last)
<ipython-input-14-7122d1244c55> in <module>()
11
12 # Load Data Sets
---> 13 training_set = tf.contrib.learn.datasets.base.load_csv_with_header(
14 filename = IRIS_TRAINING,
15 target_dtype = np.int,
AttributeError: 'module' object has no attribute 'learn'
Clearly the module has the attribute learn since tensorflow has a section on learning tf.contrib.learn. What am I doing wrong? All guidance is appreciated.