[英]Multi image classes classification using CNN Keras
I am new in the field of CNN.我是CNN领域的新手。
I want to classify 100 images.我想对 100 张图像进行分类。
These images are belong to 12 classes.这些图像属于 12 个类。
I have a csv file that contain the image ID (the ID is the image file name) and the image classes.我有一个包含图像 ID(ID 是图像文件名)和图像类的 csv 文件。
How I can use and divide the classes from the csv file into train and test.我如何使用 csv 文件中的类并将其划分为训练和测试。
I mean when I split the data into train and test, how I can divide the 100 pictures and their classes from the csv file to train and test, then feed them to the model.我的意思是,当我将数据拆分为训练和测试时,如何将 100 张图片及其类别从 csv 文件中进行训练和测试,然后将它们提供给模型。
I use python and Keras library.我使用 python 和 Keras 库。
Thanks in advance.提前致谢。
import os
import cv2
from keras.layers import Input,Dense,Flatten,Dropout,merge,Reshape,Conv3D,MaxPooling3D,UpSampling3D,Conv2DTranspose
from keras.layers.normalization import BatchNormalization
from keras.models import Model,Sequential
from keras.callbacks import ModelCheckpoint
from keras.optimizers import Adadelta, RMSprop,SGD,Adam
from keras import regularizers
from keras import backend as K
import numpy as np
import scipy.misc
import numpy.random as rng
from PIL import Image, ImageDraw, ImageFont
from sklearn.utils import shuffle
import nibabel as nib #reading MR images
from sklearn.model_selection import train_test_split
import math
import glob
from matplotlib import pyplot as plt
import pandas as pd
from google.colab import drive
drive.mount('/content/drive')
files = glob.glob('/content/drive/My Drive/im_id/*')
files[0]
len(files)
images = []
for f in range(len(files)):
a = nib.load(files[f])
a = a.get_data()
images.append(a)
print(a.shape)
images = np.asarray(images)
print(images.shape)
labeles = pd.read_csv('/content/drive/My
Drive/img_id.csv')
print(labeles)
class_names = labeles["Class"]
from tensorflow.keras import datasets, layers, models
model = models.Sequential()
model.add(layers.Conv3D(32, (3, 3, 3), activation='relu', input_shape=(110, 110, 110, 1)))
model.add(layers.MaxPooling3D((2, 2, 2)))
model.add(layers.Conv3D(64, (3, 3, 3), activation='relu'))
model.add(layers.MaxPooling3D((2, 2, 2)))
model.add(layers.Conv3D(64, (3, 3, 3), activation='relu'))
model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))
model.summary()
You need to create a tf.data.Dataset
, which is an abstraction that represents a sequence of elements, in which each element consists of one or more components.您需要创建一个
tf.data.Dataset
,它是一个表示元素序列的抽象,其中每个元素由一个或多个组件组成。 In other words, it's a generator in python.换句话说,它是 python 中的生成器。
I don't know your data, so I can't help you properly.我不知道你的数据,所以我不能正确地帮助你。 The best I can do is to give you a gist of how to do it and to share some links.
我能做的最好的事情就是给你一个关于如何做的要点并分享一些链接。
In brief, you need to read your csv
file, I suggest using pandas
, and the images' path.简而言之,您需要阅读您的
csv
文件,我建议使用pandas
和图像的路径。 Now, you can split them using NumPy
, for example, you can shuffle your data and then get the first 10% of values in the array for testing and the rest for training.现在,您可以使用
NumPy
拆分它们,例如,您可以对数据进行混洗,然后获取数组中的前 10% 的值用于测试,其余的用于训练。
Next, you need to use the tf.data
API to generate the datasets.接下来,您需要使用
tf.data
API 来生成数据集。 The method tf.data.Dataset.from_tensor_slices
do the job for you.方法
tf.data.Dataset.from_tensor_slices
为您完成这项工作。 You just need to pass the NumPy
arrays as parameters and get the generators to pass to your Keras
pipeline.您只需要将
NumPy
数组作为参数传递并让生成器传递到您的Keras
管道。 You must do that to your training and testing arrays.您必须对您的训练和测试阵列执行此操作。
I have a pipeline to do that here .我有一个管道来做到这一点。 Feel free to use it.
随意使用它。 You can also check the manual here and here
您也可以在此处和此处查看手册
I hope it helps you.我希望它能帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.