Kaggle - Cats vs dogs (UserWarning: Found 20000 invalid image filename(s) in x_col="filename". These filename(s) will be ignored.) - machine-learning

When I am generating the test generator, I get below error
UserWarning: Found 20000 invalid image filename(s) in x_col="filename". These filename(s) will be ignored.
Here is the code:
test_datagen = ImageDataGenerator(rotation_range=15,
rescale=1./255,
shear_range=0.1,
zoom_range=0.2,
horizontal_flip=True,
width_shift_range=0.1,
height_shift_range=0.1)
test_generator = train_datagen.flow_from_dataframe(train_df,
"./test/",x_col='filename',y_col='category',
target_size=Image_Size,
class_mode='categorical',
batch_size=batch_size)
The test folder is immediate before the file. I have searched a lot to fix this issue. If any thing wrong in the code please specify that or give me other suggestions.

All jpeg / png names must be in numerical order, according to the flow_from_dataframe in ImageDataGenerator, alphanumeric labels are not permitted. The image would be ignored, and a warning for that file will be issued.
Every image in your scenario has alphanumeric label (name in csv).

Related

How to Export Stanza to ONNX format?

How to export Stanza to ONNX format?
It seems impossible to just simply train the model.
There is an explanation here: https://pytorch.org/tutorials/advanced/super_resolution_with_onnxruntime.html
I created a fork from stanza for this experiment here https://github.com/vivkvv/stanza. See also my commits https://github.com/vivkvv/stanza/commits?author=vivkvv.
I used pipeline_demo.py for testing. The main thing I added is code just inside models/tokanization/trainer.py below the line 77
pred = self.model(units, features)
Due to explanation I added
torch.onnx.export(
self.model,
(units, features),
onnx_export_file_name,
opset_version=9,
export_params=True,
do_constant_folding=True,
input_names=['input'],
output_names=['output'],
dynamic_axes={
'input': {0: 'batch_size'},
'output': {0: 'batch_size'}
}
)
and it works for tokenization. But the same does not work for e.g. pos or lemmatizer (see my commit for PartOfSpeech). And I get different errors for different opset_version.
I created a question on github/stanza and you could see there https://github.com/stanfordnlp/stanza/issues/893

Create jpg/png from encrypted image

I'd like to be able to convert/display an AES256 asymmetric encrypted image even if it appears to be garbage, I've read a number of things on SO that suggest removing the headers and then reattaching them afterward so even if looks nutty it still displays it.
The point of this is that I want to see if it's possible to perform image classification on an image dataset encrypted with a known public key. If I have a picture of a cat and I encrypt it with exactly the same key, then the result will generally be reproducible and result in an image that in some way equates to the original.
Excuse the lack of code, I didn't want to pollute the discussion with ideas that I was considering in order to get a proper critique from you lovely people- I would say I'm not an encryption expert hence my asking for advice here.
There are many options, but I suggest to follow the following guidelines:
Encrypt the image data, and not the image file.
In case the image is 100x100x3 bytes, encrypt the 30000 bytes (not the img.jpg file for example).
(The down side is that metadata is not saved as part of the encrypt image).
Use lossless image file format to store the encrypted image (PNG file format for example, and not JPEG format).
Lossy format like JPEG is going to be irreversible.
Set the resolution of the encrypted image to the same resolution as the input image.
That way you don't need to store the image headers - the resolution is saved.
You may need to add padding, so the size in bytes be a multiple of 32.
I hope you know Python...
Here is a Python code sample that demonstrates the encoding and decoding procedures:
import cv2
import numpy as np
from Crypto.Cipher import AES
# https://stackoverflow.com/questions/61240967/image-encryption-using-aes-in-python
key = b'Sixteen byte key'
iv = b'0000000000000000'
# Read image to NumPy array - array shape is (300, 451, 3)
img = cv2.imread('chelsea.png')
# Pad zero rows in case number of bytes is not a multiple of 16 (just an example - there are many options for padding)
if img.size % 16 > 0:
row = img.shape[0]
pad = 16 - (row % 16) # Number of rows to pad (4 rows)
img = np.pad(img, ((0, pad), (0, 0), (0, 0))) # Pad rows at the bottom - new shape is (304, 451, 3) - 411312 bytes.
img[-1, -1, 0] = pad # Store the pad value in the last element
img_bytes = img.tobytes() # Convert NumPy array to sequence of bytes (411312 bytes)
enc_img_bytes = AES.new(key, AES.MODE_CBC, iv).encrypt(img_bytes) # Encrypt the array of bytes.
# Convert the encrypted buffer to NumPy array and reshape to the shape of the padded image (304, 451, 3)
enc_img = np.frombuffer(enc_img_bytes, np.uint8).reshape(img.shape)
# Save the image - Save in PNG format because PNG is lossless (JPEG format is not going to work).
cv2.imwrite('enctypted_chelsea.png', enc_img)
# Decrypt:
################################################################################
key = b'Sixteen byte key'
iv = b'0000000000000000'
enc_img = cv2.imread('enctypted_chelsea.png')
dec_img_bytes = AES.new(key, AES.MODE_CBC, iv).decrypt(enc_img.tobytes())
dec_img = np.frombuffer(dec_img_bytes, np.uint8).reshape(enc_img.shape) # The shape of the encrypted and decrypted image is the same (304, 451, 3)
pad = int(dec_img[-1, -1, 0]) # Get the stored padding value
dec_img = dec_img[0:-pad, :, :].copy() # Remove the padding rows, new shape is (300, 451, 3)
# Show the decoded image
cv2.imshow('dec_img', dec_img)
cv2.waitKey()
cv2.destroyAllWindows()
Encrypted image:
Decrypted image:
Idea for identifying the encrypted image:
Compute a hash of the encrypted image, and store it in your database, along the original image, the key and the iv.
When you have the encrypted image, compute the hash, and search for it in your database.
I'm using an answer, although it's not an answer because I'd like to show two pictures to demonstrate.
Both pictures were taken from my blog entry http://javacrypto.bplaced.net/g01-ecb-pinguin/ (German language).
The first picture shows the Tuc penguin after encryption with AES in ECB mode:
The form still persists, and you can "imagine" what animal is shown.
The second picture was encrypted with AES in CBC mode and the output is looking like garbage:
The conclusion: if the picture was encrypted with a mode like CBC, CTR or GCM you will always get something like the second picture, even if you know the mode, key and initialization vector that was in use.
A visual comparison will not work, sorry.
To answer your question in comment "how would you display encrypted images in their encrypted form": you can't show them because usually a picture has a header that gets encrypted as well, so this information will be lost. The two "encrypted" pictures were created by stripping off the header before encryption, then the picture data gets encrypted, and the header is prepended.

TypeError: __call__() takes 2 positional arguments but 3 were given. To train Raccoon prediction model using FastRCNN through Transfer Learning

from torchvision.models.detection.faster_rcnn import FastRCNNPredictor
from engine import train_one_epoch, evaluate
import utils
import torchvision.transforms as T
num_epochs = 10
for epoch in range(num_epochs):
train_one_epoch(model, optimizer, data_loader, device, epoch, print_freq=10)
lr_scheduler.step()
evaluate(model, data_loader_test, device=device)
I am using the same code as provided in this link Building Raccoon Model but mine is not working.
This is the error message I am getting
TypeError Traceback (most recent call last)
in ()
2 for epoch in range(num_epochs):
3 # train for one epoch, printing every 10 iterations
4 ----> train_one_epoch(model, optimizer, data_loader, device, epoch, print_freq=10)
5 # update the learning rate
6 lr_scheduler.step()
7 frames
in getitem(self, idx)
29 target["iscrowd"] = iscrowd
30 if self.transforms is not None:
31 ---> img, target = self.transforms(img, target)
32 return img, target
33
TypeError: call() takes 2 positional arguments but 3 were given
The above answer is incorrect, I accidentally upvoted before noticing. You are using the wrong Compose, note that it says
https://pytorch.org/tutorials/intermediate/torchvision_tutorial.html#putting-everything-together
"In references/detection/, we have a number of helper functions to simplify training and evaluating detection models. Here, we will use references/detection/engine.py, references/detection/utils.py and references/detection/transforms.py. Just copy them to your folder and use them here."
there are helper scripts. They subclass the compose and flip methods
https://github.com/pytorch/vision/blob/6315358dd06e3a2bcbe9c1e8cdaa10898ac2b308/references/detection/transforms.py#L17
I did the same thing before noticing this. Do not use the compose method from torchvision.transforms, or else you will get the error above. Download their module and load it.
I am kind of a newbie at this and I was also having the same problem.
Upon doing more research, I found this where the accepted answer used:
img = self.transforms(img)
instead of:
img, target = self.transforms(img, target)
Removing "target" solved the error for me and should solve it for you as well. Not entirely sure why even the official PyTorch tutorial also has "target" included but it does not work for us.
I had the same issue, there is even an issue raised on Pytorch discussion forum using regarding the same T.Compose | TypeError: call() takes 2 positional arguments but 3 were given
I was able to overcome this issue by copy and pasting the files on the for a specific version v0.3.0 on the vision/reference/detection of the tutorial I am following building-your-own-object-detector-pytorch-vs-tensorflow-and-how-to-even-get-started
Just to fall into another issue I have raised here ValueError: All bounding boxes should have positive height and width. Found invaid box [500.728515625, 533.3333129882812, 231.10546875, 255.2083282470703] for target at index 0. #2740

TensorFlow error: "logits and labels must be same size", warmspringwinds "tutorial"

I'm currently following this tutorial and after I did some changes because of the tensorflow update, I got this error:
tensorflow.python.framework.errors_impl.InvalidArgumentError: logits and labels must be same size: logits_size=[399360,2] labels_size=[409920,2]
[[Node: SoftmaxCrossEntropyWithLogits = SoftmaxCrossEntropyWithLogits[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](Reshape_2, Reshape_3)]].
Can anyone help me with this one?
Changes in the code:
#Replaced concat_dim=2 with axis=2
combined_mask = tf.concat(axis=2, values=[bit_mask_class, bit_mask_background])
#Update the import of urllib2 to urllib3
#Replace tf.pack with tf.stack
upsampled_logits_shape = tf.stack([
downsampled_logits_shape[0],
downsampled_logits_shape[1] * upsample_factor,
downsampled_logits_shape[2] * upsample_factor,
downsampled_logits_shape[3]])
The error is raised because the number of logits is 399360 while you are providing to the function with 409920 labels. The function tf.nn.softmax_cross_entropy_with_logits expects one label for each logit, and it crashes because you are providing more labels than logits.
As to why it happens, you should post the changes you made to the code.

Using Keras ImageDataGenerator in a regression model

I want to use the flow_from_directory method of the ImageDataGenerator
to generate training data for a regression model, where the target value can be any float value between 1 and -1. flow_from_directory has a "class_mode" parameter with the description
class_mode: one of "categorical", "binary", "sparse" or None. Default:
"categorical". Determines the type of label arrays that are returned:
"categorical" will be 2D one-hot encoded labels, "binary" will be 1D
binary labels, "sparse" will be 1D integer labels.
Which of these values should I take? None of them seems to really fit...
With Keras 2.2.4 you can use flow_from_dataframe which solves what you want to do, allowing you to flow images from a directory for regression problems. You should store all your images in a folder and load a dataframe containing in one column the image IDs and in the other column the regression score (labels) and set class_mode='other' in flow_from_dataframe.
Here you can find an example where the images are in image_dir, the dataframe with the image IDs and the regression scores is loaded with pandas from the "train file"
train_label_df = pd.read_csv(train_file, delimiter=' ', header=None, names=['id', 'score'])
train_datagen = ImageDataGenerator(rescale = 1./255, horizontal_flip = True,
fill_mode = "nearest", zoom_range = 0.2,
width_shift_range = 0.2, height_shift_range=0.2,
rotation_range=30)
train_generator = train_datagen.flow_from_dataframe(dataframe=train_label_df, directory=image_dir,
x_col="id", y_col="score", has_ext=True,
class_mode="other", target_size=(img_width, img_height),
batch_size=bs)
I think that organizing your data differently, using a DataFrame (without necessarily moving your images to new locations) will allow you to run a regression model. In short, create columns in your DataFrame containing the file path of each image and the target value. This allows your generator to keep regression values and images properly synced even when you shuffle your data at each epoch.
Here is an example showing how to link images with binomial targets, multinomial targets and regression targets just to show that "a target is a target is a target" and only the model might change:
df['path'] = df.object_id.apply(file_path_from_db_id)
df
object_id bi multi path target
index
0 461756 dog white /path/to/imgs/756/61/blah_461756.png 0.166831
1 1161756 cat black /path/to/imgs/756/61/blah_1161756.png 0.058793
2 3303651 dog white /path/to/imgs/651/03/blah_3303651.png 0.582970
3 3367756 dog grey /path/to/imgs/756/67/blah_3367756.png -0.421429
4 3767756 dog grey /path/to/imgs/756/67/blah_3767756.png -0.706608
5 5467756 cat black /path/to/imgs/756/67/blah_5467756.png -0.415115
6 5561756 dog white /path/to/imgs/756/61/blah_5561756.png -0.631041
7 31255756 cat grey /path/to/imgs/756/55/blah_31255756.png -0.148226
8 35903651 cat black /path/to/imgs/651/03/blah_35903651.png -0.785671
9 44603651 dog black /path/to/imgs/651/03/blah_44603651.png -0.538359
10 49557622 cat black /path/to/imgs/622/57/blah_49557622.png -0.295279
11 58164756 dog grey /path/to/imgs/756/64/blah_58164756.png 0.407096
12 95403651 cat white /path/to/imgs/651/03/blah_95403651.png 0.790274
13 95555756 dog grey /path/to/imgs/756/55/blah_95555756.png 0.060669
I describe how to do this in great detail with examples here:
https://techblog.appnexus.com/a-keras-multithreaded-dataframe-generator-for-millions-of-image-files-84d3027f6f43
At this moment (newest version of Keras from January 21st 2017) the flow_from_directory could only work in a following manner:
You need to have a directories structured in a following manner:
directory with images\
1st label\
1st picture from 1st label
2nd picture from 1st label
3rd picture from 1st label
...
2nd label\
1st picture from 2nd label
2nd picture from 2nd label
3rd picture from 2nd label
...
...
flow_from_directory returns batches of a fixed size in a format of (picture, label).
So as you can see it could only be used for a classification case and all options provided in a documentation specify only a way in which the class is provided to your classifier. But, there is a neat hack which could make a flow_from_directory useful for a regression task:
You need to structure your directory in a following manner:
directory with images\
1st value (e.g. -0.95423)\
1st picture from 1st value
2nd picture from 1st value
3rd picture from 1st value
...
2nd value (e.g. - 0.9143242)\
1st picture from 2nd value
2nd picture from 2nd value
3rd picture from 2nd value
...
...
You also need to have a list list_of_values = [1st value, 2nd value, ...]. Then your generator is defined in a following manner:
def regression_flow_from_directory(flow_from_directory_gen, list_of_values):
for x, y in flow_from_directory_gen:
yield x, list_of_values[y]
And it's crucial for a flow_from_directory_gen to have a class_mode='sparse' to make this work. Of course this is a little bit cumbersome but it works (I used this solution :) )
There's just one glitch in the accepted answer that I would like to point out. The above code fails with an error message like:
TypeError: only integer scalar arrays can be converted to a scalar index
This is because y is an array. The fix is simple:
def regression_flow_from_directory(flow_from_directory_gen,
list_of_values):
for x, y in flow_from_directory_gen:
values = [list_of_values[y[i]] for i in range(len(y))]
yield x, values
The method to generate the list_of_values can be found in https://stackoverflow.com/a/47944082/4082092

Resources