import pickle
from tensorflow.keras.layers import Dense,Flatten,Conv2D,Activation,Activation,MaxPooling2D,Dropout
from tensorflow.keras.models import Sequential
from keras.utils import to_categorical
import numpy as np
X = np.array(pickle.load(open('X.pickle','rb')))
y = np.array(pickle.load(open('y.pickle','rb')))
test_X = np.array(pickle.load(open('test_X.pickle','rb')))
test_y = np.array(pickle.load(open('test_y.pickle','rb')))
#normalizing the data
X = X/255.0
test_X = test_X/255.0
model = Sequential()
#input layer
model.add(Conv2D(32,(3,3),input_shape=X.shape[1:]))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
#second layer
model.add(Conv2D(64,(3,3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(128,(3,3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
#third layer
model.add(Flatten())
model.add(Dense(1000,activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(500,activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(250,activation='relu'))
#output layer
model.add(Dense(140))
model.add(Activation('softmax'))
model.compile(
loss='sparse_categorical_crossentropy',optimizer='adam',metrics=['accuracy']
)
model.fit(X,y,batch_size=128,epochs=10)
loss,accuracy = model.evaluate(
test_X,
to_categorical(test_y)
)
print(loss,accuracy)
上面这个是我的CNN源代码,第二个是我的数据预处理源代码。我一直在做各种改变,但我是机器学习的新手,所以我不太了解我在做什么,但我有基础。我的uput层使用了140个神经元,这是我的物种类别的数量。
import numpy as np
import matplotlib.pyplot as plt
import os
import cv2
import random
import pickle
BASE_DIR = 'C:\datasets\100-bird-species'
CATEGORIES = ['ALBATROSS', 'ALEXANDRINE PARAKEET', 'AMERICAN AVOCET', 'AMERICAN BITTERN', 'AMERICAN COOT', 'AMERICAN GOLDFINCH', 'AMERICAN KESTREL', 'AMERICAN REDSTART', 'ANHINGA', 'ANNAS HUMMINGBIRD', 'BALD EAGLE', 'BALTIMORE ORIOLE', 'BANANAQUIT', 'BAR-TAILED GODWIT', 'BARN OWL', 'BARN SWALLOW', 'BAY-BREASTED WARBLER', 'BELTED KINGFISHER', 'BIRD OF PARADISE', 'BLACK FRANCOLIN', 'BLACK SKIMMER', 'BLACK SWAN', 'BLACK-CAPPED CHICKADEE', 'BLACK-NECKED GREBE', 'BLACKBURNIAM WARBLER', 'BLUE HERON', 'BOBOLINK', 'BROWN THRASHER', 'CACTUS WREN', 'CALIFORNIA CONDOR', 'CALIFORNIA GULL', 'CALIFORNIA QUAIL', 'CAPE MAY WARBLER', 'CHARA DE COLLAR', 'CHIPPING SPARROW', 'CINNAMON TEAL', 'COCK OF THE ROCK', 'COCKATOO', 'COMMON GRACKLE', 'COMMON LOON', 'COMMON POORWILL', 'COMMON STARLING', 'COUCHS KINGBIRD', 'CRESTED AUKLET', 'CRESTED CARACARA', 'CROW', 'CROWNED PIGEON', 'CURL CRESTED ARACURI', 'DARK EYED JUNCO', 'DOWNY WOODPECKER', 'EASTERN BLUEBIRD', 'EASTERN ROSELLA', 'EASTERN TOWEE', 'ELEGANT TROGON', 'EMPEROR PENGUIN', 'EVENING GROSBEAK', 'FLAME TANAGER', 'FLAMINGO', 'FRIGATE', 'GLOSSY IBIS', 'GOLD WING WARBLER', 'GOLDEN CHLOROPHONIA', 'GOLDEN EAGLE', 'GOLDEN PHEASANT', 'GOULDIAN FINCH', 'GRAY CATBIRD', 'GRAY PARTRIDGE', 'GREEN JAY', 'GREY PLOVER', 'HAWAIIAN GOOSE', 'HOODED MERGANSER', 'HOOPOES', 'HOUSE FINCH', 'HOUSE SPARROW', 'HYACINTH MACAW', 'INDIGO BUNTING', 'JABIRU', 'LARK BUNTING', 'LILAC ROLLER', 'LONG-EARED OWL', 'MALLARD DUCK', 'MANDRIN DUCK', 'MARABOU STORK', 'MOURNING DOVE', 'MYNA', 'NICOBAR PIGEON', 'NORTHERN CARDINAL', 'NORTHERN FLICKER', 'NORTHERN GOSHAWK', 'NORTHERN JACANA', 'NORTHERN MOCKINGBIRD', 'NORTHERN RED BISHOP', 'OSPREY', 'OSTRICH', 'PAINTED BUNTIG', 'PARADISE TANAGER', 'PARUS MAJOR', 'PEACOCK', 'PELICAN', 'PEREGRINE FALCON', 'PINK ROBIN', 'PUFFIN', 'PURPLE FINCH', 'PURPLE GALLINULE', 'PURPLE MARTIN', 'PURPLE SWAMPHEN', 'QUETZAL', 'RAINBOW LORIKEET', 'RED FACED CORMORANT', 'RED HEADED WOODPECKER', 'RED THROATED BEE EATER', 'RED WINGED BLACKBIRD', 'RED WISKERED BULBUL', 'RING-NECKED PHEASANT', 'ROADRUNNER', 'ROBIN', 'ROSY FACED LOVEBIRD', 'ROUGH LEG BUZZARD', 'RUBY THROATED HUMMINGBIRD', 'SAND MARTIN', 'SCARLET IBIS', 'SCARLET MACAW', 'SNOWY EGRET', 'SPLENDID WREN', 'STORK BILLED KINGFISHER', 'STRAWBERRY FINCH', 'TEAL DUCK', 'TIT MOUSE', 'TOUCHAN', 'TRUMPTER SWAN', 'TURKEY VULTURE', 'TURQUOISE MOTMOT', 'VARIED THRUSH', 'VENEZUELIAN TROUPIAL', 'VERMILION FLYCATHER', 'VIOLET GREEN SWALLOW', 'WESTERN MEADOWLARK', 'WILSONS BIRD OF PARADISE', 'WOOD DUCK', 'YELLOW HEADED BLACKBIRD']
IMG_SIZE = 50
#returns a 50X50 image and a number label
def get_and_preprocess_data(SUB_DIR,IMG_SIZE=50):
FULL_DIR = BASE_DIR+'\'+SUB_DIR
data = []
for category in CATEGORIES:
class_num = CATEGORIES.index(category)/145 #to ensure working well with model
path = os.path.join(FULL_DIR,category)
#In case of a bad image
for image in os.listdir(path):
try:
img_array = cv2.imread(os.path.join(path,image),cv2.IMREAD_GRAYSCALE)
new_array = cv2.resize(img_array,(IMG_SIZE,IMG_SIZE))
data.append([new_array,class_num])
except Exception as e:
print(e)
pass
'''
#show one of the images
plt.imshow(new_array,cmap='gray')
plt.show()
break
break'''
return data
def final_processing(data,name_x,name_y):
X= []
y =[]
for features,labels in data:
X.append(features)
y.append(labels)
X = np.array(X).reshape(-1,IMG_SIZE,IMG_SIZE,1)
#saving current state of dataset to prevent constant reloading
pickle_out = open(f'{name_x}.pickle','wb')
pickle.dump(X,pickle_out)
pickle_out.close()
pickle_out = open(f'{name_y}.pickle','wb')
pickle.dump(y,pickle_out)
pickle_out.close()
#also make sure that all categories have the same number of items because if one greatly
#outweights the other, it will cause a decision error(It will favor the category that is more)
#shuffle it so the network doesn't read one type of
#data first then another and so learn wrongly
'''
train = get_and_preprocess_data('train')
random.shuffle(train)
final_processing(train,'X','y')
test = get_and_preprocess_data('test')
random.shuffle(test)
final_processing(test,'test_X','test_y')'''
可能出了什么问题。我正试图在一个包含140种鸟类的数据集上进行分类
您可以执行以下操作:
- 增加纪元
- 玩批量大小
- 如果你的唯一目标是结果,请使用预先训练过的模型
另一个选择是增加模型大小并添加batchnorm层!还要确保你的数据没有不平衡!
要检查模型的确切位置,请使用此XAI库:https://github.com/datamllab/xdeep