• 大小: 29.46MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-07-11
  • 语言: 其他
  • 标签: python  tensorflow  keras  

资源简介

服装识别项目数据集与代码

资源截图

代码片段和文件信息

import tensorflow as tf
from tensorflow import keras
import osgzip
import numpy as np
import matplotlib.pyplot as plt
fashion_mnist=keras.datasets.fashion_mnist
path=‘./data‘
files=[‘train-labels-idx1-ubyte.gz‘
       ‘train-images-idx3-ubyte.gz‘
       ‘t10k-labels-idx1-ubyte.gz‘
       ‘t10k-images-idx3-ubyte.gz‘]
def load_data(data_folderfiles):
    paths=[]
    for fname in files:
        paths.append(os.path.join(data_folderfname))
    ##frombuffer将data以流的形式读入转化成ndarray对象
    #第一参数为stream第二参数为返回值的数据类型,第三参数指定从stream的第几位开始读入
    with gzip.open(paths[0]‘rb‘) as lbpath:
        y_train = np.frombuffer(lbpath.read()np.uint8offset=8)
    with gzip.open(paths[1]‘rb‘) as imgpath:
        x_train = np.frombuffer(imgpath.read()np.uint8offset=16).reshape(len(y_train)2828)
    with gzip.open(paths[2] ‘rb‘) as lbpath:
        y_test = np.frombuffer(lbpath.read() np.uint8 offset=8)
    with gzip.open(paths[3]‘rb‘) as imgpath:
        x_test = np.frombuffer(imgpath.read()np.uint8offset=16).reshape(len(y_test)2828)
    return (x_trainy_train)(x_testy_test)
(train_imagestrain_labels)(test_imagestest_labels)=load_data(pathfiles)
class_names = [‘T-shirt/top‘‘Trouser‘‘Pullover‘‘Dress‘‘Coat‘‘Sandal‘‘Shirt‘‘Sneaker‘‘Shirt‘‘Sneaker‘‘Bag‘‘Ankle boot‘]
train_images=train_images/255.0
test_images=test_images/255.0
plt.figure(figsize=(1010))
for i in range(25):
    plt.subplot(55i+1)
    plt.xticks([])
    plt.yticks([])
    plt.grid(False)
    plt.imshow(train_images[i]cmap=plt.cm.binary)
    plt.xlabel(class_names[train_labels[i]])
# plt.show()
#建模
model=keras.Sequential([
    keras.layers.Flatten(input_shape=(2828))
    keras.layers.Dense(128activation=‘relu‘)
    keras.layers.Dense(10activation=‘softmax‘)])
#编译训练模型
model.compile(optimizer=‘adam‘
              loss=‘sparse_categorical_crossentropy‘
              metrics=[‘accuracy‘])
model.fit(train_imagestrain_labelsepochs=10)
#评估模型及预测
test_losstest_acc = model.evaluate(test_imagestest_labelsverbose=2)
print(‘\nTest accuracy:‘test_acc)
predictions = model.predict(test_images)
print(predictions[1])
print(np.argmax(predictions[1]))
print(test_labels[1])
def plot_image(ipredictions_arraytrue_labelimg):
    predictions_arraytrue_labelimg=predictions_arraytrue_label[i]img[i]
    plt.grid(False)
    plt.xticks([])
    plt.yticks([])
    plt.imshow(imgcmap=plt.cm.binary)
    predicted_label=np.argmax(predictions_array)
    if predicted_label == true_label:
        color = ‘blue‘
    else:
        color=‘red‘
    plt.xlabel(‘{}{:2.0f}%({})‘.format(class_names[predicted_label]
                                       100*np.max(predictions_array)
                                       class_names[true_label])
               color=color)
def plot_value_array(ipredictions_arraytrue_label):
    predictions_arraytrue_label=predictions_arraytrue_label[i]
    plt.grid(False)
    plt.xt

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件           0  2020-01-15 09:17  yi_shibie\__init__.py
     目录           0  2020-01-15 09:22  yi_shibie\data\
     文件     4422102  2020-01-15 09:20  yi_shibie\data\t10k-images-idx3-ubyte.gz
     文件        5148  2020-01-15 09:20  yi_shibie\data\t10k-labels-idx1-ubyte.gz
     文件    26421880  2020-01-15 09:21  yi_shibie\data\train-images-idx3-ubyte.gz
     文件       29515  2020-01-15 09:20  yi_shibie\data\train-labels-idx1-ubyte.gz
     文件        3994  2020-01-15 14:20  yi_shibie\yi_shibie.py

评论

共有 条评论