Loading Custom Dataset into TensorFlow CNN - python

We are using TensorFlow and python to create a custom CNN that will classify images into one of several categories. We have created our CNN based on this tutorial: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/tutorials/layers/cnn_mnist.py
Instead of reading in a pre-existing dataset like the MNIST dataset used in the tutorial, we would like to read in all images from multiple folders. The name of each folder is the label associated with all the images in that folder. Unfortunately we're very new to python and TensorFlow, could someone point us in the right direction, either with a tutorial or some basic code?
Thank you so much!

consider using the glob package. it allows you to import multiple files in subdirectories easily using patterns. https://docs.python.org/2/library/glob.html
import glob
import matplotlib.pyplot as plt
import numpy as np
images = glob.glob(<file pattern>)
img_list = [plt.imread(image) for image in images]
img_array = np.stack(tuple(img_list))
I haven't tested this so there might be errors but it should make a 3-d numpy array of images (each image a 2-d array). Is this the format you were looking for?

Related

loading all images with different names from drive in python

Multiple images are stored in my google drive folder. All images have different names. I want to load them all for my deep learning project. All loaded images should be of type numpy array. What can I do?
Here is the code that I have implemented
import cv2
loc="" #My drive directory location
I have successfully loaded one images using
img=cv2.imread(str(loc)+"image_name.png",1)
I am stuck here, because names of all images are different
You can get all image names using os.listdir('you image folder path'). You can do something like this:
import os,cv2
for image_path in os.listdir('image folder path'):
cv2.imread(base_path + image_path,1)

How to generate the captcha to train with Python

I would like to use deep learning program for recognizing the captcha using keras with python.
But the big challenge is to generate massive captcha to train.
I want to solve a captcha like this
How can i easily generate above massive captcha to train.
Currently, i use python package captcha
from captcha.image import ImageCaptcha # pip install captcha
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import random
import os
number = ['0','1','2','3','4','5','6','7','8','9']
MAX_CAPTCHA = 6
WIDTH=100
HEIGHT=30
image = ImageCaptcha(width=WIDTH, height=HEIGHT, font_sizes=[30])
captcha_text = []
for i in range(MAX_CAPTCHA):
c = random.choice(number)
captcha_text.append(c)
#print(captcha_text)
captcha_text = ''.join(captcha_text)
print(captcha_text)
captcha = image.generate(captcha_text)
captcha_image = Image.open(captcha)
captcha_image = np.array(captcha_image)
image.write(captcha_text, str(i)+'_'+captcha_text + '.png')
plt.imshow(captcha_image)
plt.show()
If there is no existing similar captcha datasets online I would tackle this problem in the following way:
Get the MNIST dataset
Take one image example and play with it in gimp or some image transformation librari like Open CV, to get a look similar to your captcha examples.
Transform the MNIST examples in a way you find fit (some random noise, and randomized color on black pixels or something)
Train a model on these augmented examples
Now for practical use it depends a little what kind of model you are implementing. If you have a model that can detect and classify all the numbers on the image then you are finished. But if you like to have a simple model that classifies only images with a single digit on them, then you can move a sliding window over your captcha image and only collect outputs of windows for wich the model has high enough confidence that there is some number pressent in the window.
You can generate captchas using PIL python Imaging Library
Another solution may be using Inkscape and its python scripting api.
There are some program already available using these technologies:
https://github.com/kuszaj/claptcha
https://github.com/ramwin/captcha
https://www.quora.com/What-is-the-code-for-generating-an-image-CAPTCHA-using-Python

Resize CSV data using Python and Keras

I have CSV files that I need to feed to a Deep-Learning network. Currently my CSV files are of size 360*480, but the network restricts them to be of size 224*224. I am using Python and Keras for the deep-learning part. So how can I resize the matrices?
I was thinking that since aspect ratio is 3:4, so if I resize them to 224:(224*4/3) = 224:299, and then crop the width of the matrix to 224, it could serve the purpose. But I cannot find a suitable function to do that. Please suggest.
I think you're looking for cv.resize() if you're using images.
If not, try numpy.ndarray.resize()
Image processing
If you want to do nontrivial alterations to the data as images (i.e. interpolating between pixel values, assuming that they represent photographs) then you might want to use proper image processing libraries for that. You'd need to treat them not as raw matrixes (csv of numbers) but convert them to rgb images, do the transformations you desire, and convert them back to a numpy matrix.
OpenCV (https://docs.opencv.org/3.4/da/d6e/tutorial_py_geometric_transformations.html)
or Pillow (https://pillow.readthedocs.io/en/3.1.x/reference/Image.html) might be useful to do that.
I found a short and simple way to solve this. This uses the Python Image Library/Pillow.
import numpy as np
import pylab as pl
from PIL import Image
matrix = np.array(list(csv.reader(open('./path/mat.csv', "r"), delimiter=","))).astype("uint8") #read csv
imgObj = Image.fromarray(matrix) #convert matrix to Image object
resized_imgObj = img.resize((224,224)) #resize Image object
imgObj.show()
resized_imgObj.show()
resized_matrix = np.asarray(img) #convert Image object to matrix
While numpy module also has a resize function, but it is not as useful as the aforementioned way.
When I tried it, the resized matrix had lost all the intricacies and aesthetic aspect of the original matrix. This is probably due to the fact that numpy.ndarray.resize doesn't interpolate and missing entries are filled with zeros.
So, for this case Image.resize() is more useful.
You could also convert the csv file to a list, truncate the list, and then convert the list to a numpy array and then use np.reshape.

Medical imaging (PyRadiomics) with .nii.gz files

I am trying to implement the package:
https://pyradiomics.readthedocs.io/en/latest/usage.html
It looks super simple, but they expect .nrrd files.
My files are .nii.gz. How do I solve this?
Also, have anyone tried to apply PyRadiomics on TCIA data? if so, can I see your github or Jupyter Notebook?
Thanks a lot.
You could turn NII into numpy array firstly and then turn it into NRRD with using:
nrrd and nibabel
import numpy as np
import nibabel as nib
import nrrd
# Download NII
example_filename = "image.nii.gz"
image = nib.load(example_filename)
# Turn into numpy array
array = np.array(img.dataobj)
# Save NRRD
nrrd_path_to = "image.nrrd"
nrrd.write(image_path_to, array)
Although the examples are in .nrrd, PyRadiomics uses SimpleITK for image operations. This allows PyRadiomics to support a whole range of image formats, including .nii.gz. You don't have to convert them.
The DWIConverter converts diffusion-weighted MR images in DICOM series into nrrd format for analysis in Slicer. It parses the DICOM header to extract necessary information about measurement frame, diffusion weighting directions, b-values, etc, and write out a nrrd image. For non-diffusion weighted DICOM images, it loads in an entire DICOM series and writes out a single DICOM volume in a .nhdr/.raw pair.
So that trying to convert your .nii.gz inside DICOM files for the nrrd format is a possibility by using this tools. Also, you can look at the SlicerDMRI that is a similar module.

How to extract patches from a .tiff file to feed as input to Alexnet implemented in Keras?

I am trying to use transfer learning to extract features. My dataset has images in .tiff format.How do I feed patches of this format to the Alexnet implemented in Keras ?
Are you able to feed any other format of data to ALextNet or do you need complete guide to feed images into a neural network in Keras. If you are able to feed a numpy array containing the pixel values of images then you can follow the following steps to read a tiff image and convert a numpy array out of it. Or if you want a complete tutorial then you should mention it in the question.
Using PIL library
from PIL import Image
im = Image.open('a_image.tif')
Using matplotlib
import matplotlib.pyplot as plt
I = plt.imread(tiff_file)
Let us know if you need any help with it.

Categories