How to load image dataset for SVM image classification task - python

I'm trying to make a linear SVM classifier (AD vs NC) for the classification of Alzheimer's Disease by using MRI images. How can I load the image dataset correctly?
I found an example of SVM image classification and I tried to run through the trial, but there was an error when loading the dataset.
The folder name is "images"
there are five subfolders in "images". They are named as doller_bill, sunflower, pizza, dog, and ball. Each subfolder contains 50-60 photos as jpg format. The followings are the sample codes I downloaded.
download from github
from pathlib import Path
import matplotlib.pyplot as plt
import numpy as np %matplotlib notebook
from sklearn import svm, metrics, datasets
from sklearn.utils import Bunch
from sklearn.model_selection import GridSearchCV, train_test_split
from skimage.io import imread
from skimage.transform import resize
def load_image_files(container_path, dimension=(64, 64)):
image_dir = Path(container_path)
folders = [directory for directory in image_dir.iterdir() if
directory.is_dir()]
categories = [fo.name for fo in folders]
descr = "A image classification dataset"
images = []
flat_data = []
target = []
for i, direc in enumerate(folders):
for file in direc.iterdir():
img = skimage.io.imread(file)
img_resized = resize(img, dimension, anti_aliasing=True,
mode='reflect')
flat_data.append(img_resized.flatten())
images.append(img_resized)
target.append(i)
flat_data = np.array(flat_data)
target = np.array(target)
images = np.array(images)
return Bunch(data=flat_data,
target=target,
target_names=categories,
images=images,
DESCR=descr)
image_dataset = load_image_files("images/")
However, when I run through the codes, it appeared an error as follows
NameError: name 'skimage' is not defined
So, would you please help me to figure out how to load the image dataset.
For instance, I have a folder named "images"
the subfolders are named as "MRI images_NC", "MRI images_AD",
Accordingly, each folder contains 1500 photos approximately.
Thanks again.

name 'skimage' is not defined
means that during the import
from skimage.io import imread `enter code here`
the skimage package can not be found
Please run a
pip install scikit-image

Related

Sentence Transformers can not get a lot of images' embeddings

When I try to get embeddings from images I get error like this 'too many open files'. I have 50000 images, I do not want to split images into different folders and then concatenate embeddings (It is possible decision), I want to get embdedding using one folder where I have 50000 images. How to solve this problem?
from sentence_transformers import SentenceTransformer, util
from PIL import Image
#Load CLIP model
model = SentenceTransformer('clip-ViT-B-32')
#Encode an image:
img_emb = model.encode([Image.open(filepath) for filepath in image_names], batch_size=128, convert_to_tensor=True, show_progress_bar=True)

python about train a deep neural network on the MRI slices dataset

I want to Train a deep neural network on the MRI slices dataset. Here is my code
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
import matplotlib
file_dir = 'C:\\Users\\adam\\Downloads\\MRI_Images\\'
import glob
import cv2
images = [cv2.imread(file) for file in glob.glob("C:\\Users\\adam\\Downloads\\MRI_Images\\.png")]
(X_train_full, y_train_full), (X_test, y_test) = images
And python shows that not enough values to unpack. I don't know why. Is there problem when I put all images in one file to python?
I don't know the structure of your dataset directory, but I know that using glob.glob() will return all the images inside the 'C:\\Users\\adam\\Downloads\\MRI_Images\\' folder (not include subfolder).
That is, what you get inside image is a list of read-in images (numpy array format), like:
[image_0, image_1, ...]
A list can not be unpack into two tuples. And this is why the error comes out.
Try reading your train and test images seperately might help:
images_trainx = [cv2.imread(file) for file in glob.glob("C:\\Users\\adam\\Downloads\\MRI_Images\\trainx\\*.png")]
images_trainy = [cv2.imread(file) for file in glob.glob("C:\\Users\\adam\\Downloads\\MRI_Images\\trainy\\*.png")]
images_testx = [cv2.imread(file) for file in glob.glob("C:\\Users\\adam\\Downloads\\MRI_Images\\testx\\*.png")]
images_testy = [cv2.imread(file) for file in glob.glob("C:\\Users\\adam\\Downloads\\MRI_Images\\testy\\*.png")]
This approach is clunky but hard to go wrong.

Image count is zero

I am trying to create image classification model using tensorflow lite for android app here: https://www.tensorflow.org/tutorials/images/classification However,
I am using my local directory.
Here is my code:
import matplotlib.pyplot as plt
import numpy as np
import os
import PIL
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.models import Sequential
import pathlib
data_dir = pathlib.Path('C:/Users/aalroumi/Documents/MLKIT/V')
image_count = len(list(data_dir.glob('*/*.JPEG')))
print(image_count)
and here is an image of the output:
![][1]
and this is the image of my local directory:
So why am I getting image size is zero?
Am I missing something? I did everything right.
Because I am trying to files.download('model.tflite')
Image size is zero error:
Thank you
It seems you have images directly in data_dir so you should use *.JPEG to get them in glob()
But from_folder(data_dir) expects images in subfolders which names will be used as labels for images.
ie.
data_dir/cat/small_cat.JPEG
data_dir/cat/big_cat.JPEG
# ...
data_dir/dog/white_dog.JPEG
data_dir/dog/black_dog.JPEG
but you don't have subfolders so it can't find images in subfolders.
It doesn't count images directly in data_dir

How to reshape images in folder for DCGAN code?

These are the 9600 images in my folder + their properties: enter image description here
I tried resizing them with this code below, but then this gave me an image shape of (9600, 224, 672). I mainly used this code because it helped me get my images into the correct array/ I had used this code earlier for a kmeans thing.
I need a smaller image shape for the DCGAN code (something closer to 9600,64,128)-- see below for why.
import numpy as np
from numpy import save
import tensorflow as tf
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn.metrics import silhouette_score
import cv2
import os, glob, shutil
input_dir = '/Users/User/Documents/MATLAB/attempt6images/'
glob_dir = input_dir + '/*.jpg'
img = [cv2.resize(cv2.imread(file), (224, 224)) for file in glob.glob(glob_dir)]
#save('SV_images.npy', images)
paths = [file for file in glob.glob(glob_dir)]
img = np.array(np.float32(img).reshape(len(img), -1)/255)
save('SV_images.npy', img)
I'm trying to run them through a DCGAN code which was originally created for an image with shape (9600, 64, 128). When I use the image shape 224,672, my kernel dies. So I'm assuming my image size might be too big in comparison to the original images. By the way I changed the 25624 part to 256721 to match my image shape + changed all the other numbers besides the 256 column). Below is a part of that original code for the 64x128. Any idea how to reshape my image size so that my kernel doesnt die?
enter image description here

How to load an image dataset in scikit-learn?

I have collected a group of images that I want to train a model on.
How do I load the image dataset? I have a folder of training data with two folders in it denoting the two different kinds of objects. How would I go about loading this data set and then training a model?
this might help you to load your dataset into data variable from a single folder of images
import cv2
import os
import numpy as np
path = 'path to your dataset'
list_of_files = os.listdir(path)
data = np.empty(0)
for i in list_of_files:
x = cv2.imread(os.path.join(path+i))
data.append(x)

Categories