pytorch 使用FastAI训练模型时准确性非常差

eqoofvh9  于 2023-01-26  发布在  其他
关注(0)|答案(1)|浏览(156)

我目前正在构建一个模型,对不同汽车制造商的数千种车型进行分类。我阅读了fastbook的一些章节,以获得代码结构并构建我的模型。问题是,就准确性而言,我得到了绝对灾难性的结果,我已经到了训练阶段的末尾,但我意识到预测非常糟糕(这里的指标是error_rate)x1c 0d1x
下面是我目前使用的代码:

#!/usr/bin/env python
# coding: utf-8

# In[ ]:

#Affiche les class d'images peu fourni pour permettre de les enrichir
import os
import re

def count_files(directory, n_occurrences):
    files_count = {}
    for subdir, dirs, files in os.walk(directory):
        for file in files:
            filename = file.split("_", 3)[0:3] # prends les 3 premières parties du nom de fichier
            filename = "_".join(filename) 
            if filename in files_count:
                files_count[filename] += 1
            else:
                files_count[filename] = 1
    filtered_files = [(file, count) for file, count in files_count.items() if count < n_occurrences]
    filtered_files.sort(key=lambda tup: tup[1], reverse=True)
    print(filtered_files)
    
count_files('images/archive', 5)

# In[ ]:

#Nomme les classes pour le datablock
import re

def get_name(string):
  matches = re.findall(r"_", string)
  if len(matches) >= 3:
    index = string.index("_", string.index("_", string.index("_")+1)+1)
    return string[:index]
  return string

# In[ ]:

from fastai.vision.all import *

cars = DataBlock(blocks = (ImageBlock, CategoryBlock),
                 get_items=get_image_files, 
                 splitter=RandomSplitter(valid_pct=0.3, seed=44),
                 get_y=using_attr(get_name, 'name'),
                 item_tfms=Resize(128),
                 batch_tfms=aug_transforms(size=128))

# In[ ]:

dls = cars.dataloaders('images/archive', bs=16)

# In[ ]:

dls.show_batch(nrows=3, ncols=3)

# In[ ]:

model = xresnet34(n_out=dls.c)
learn = Learner(dls, model, loss_func=LabelSmoothingCrossEntropy(), metrics=accuracy)
learn.fine_tune(3)

# In[ ]:

#width 600
interp = ClassificationInterpretation.from_learner(learn)

#Matrice de confusion
#interp.plot_confusion_matrix(figsize=(12,12), dpi=60)

interp.most_confused(min_val=50)

# In[ ]:

lr_min,lr_steep = learn.lr_find(suggest_funcs=(minimum, steep))

print(f"Minimum/10: {lr_min:.2e}, steepest point: {lr_steep:.2e}")

# In[ ]:

#MinimumLR/10
learn.fit_one_cycle(3, 1e-8)

# In[ ]:

learn.unfreeze()

# In[ ]:

learn.lr_find()

# In[ ]:

#Lr_find[0]
learn.fit_one_cycle(6, lr_max=6e-5)

# In[ ]:

learn.fit_one_cycle(3, 6e-5)
learn.unfreeze()
learn.fit_one_cycle(12, lr_max=slice(1e-6,1e-4))

# In[ ]:

learn.recorder.plot_loss()

# In[ ]:

learn.predict('test/porsche.jpg')

# In[ ]:

我真的很感激任何见解和帮助!

whlutmcx

whlutmcx1#

如果您尝试使用RandomForestClassifier会怎么样?
首先定义x和y

from sklearn.ensemble import RandomForestClassifier
 from sklearn.model_selection import train_test_split
 from sklearn import metrics

X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2, random_state=1)
model = RandomForestClassifier(random_state=1)
model.fit(X_train, y_train)
preds = model.predict(X_test)

然后你试试准确度:

accuracy = metrics.accuracy_score(y_test, preds)
accuracy

相关问题