一、AI Studio 使用简介

1、官网:https://aistudio.baidu.com/aistudio/index

2、创建数据集:

用百度 AI Studio完成猫狗识别-冯金伟博客园

 3、创建项目:

用百度 AI Studio完成猫狗识别-冯金伟博客园

 用百度 AI Studio完成猫狗识别-冯金伟博客园

 二、环境搭建

1、以下操作都是在终端-1进行

用百度 AI Studio完成猫狗识别-冯金伟博客园

 2、具体步骤

下载:
wget -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-4.7.12.1-Linux-x86_64.sh

用百度 AI Studio完成猫狗识别-冯金伟博客园

安装:
bash Miniconda3-4.7.12.1-Linux-x86_64.sh
 用百度 AI Studio完成猫狗识别-冯金伟博客园
激活conda:
source miniconda3/bin/avtivate
 
更换清华源:
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ 
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/ 
conda config --set show_channel_urls yes
 
激活conda
source miniconda3/bin/activate
 
创建环境
conda create --name Env_PyTorch python=3.7
 
进入创建好的环境
conda activate Env_PyTorch 
用百度 AI Studio完成猫狗识别-冯金伟博客园

3、安装TensorFlow

install tensorflow==1.15.0

用百度 AI Studio完成猫狗识别-冯金伟博客园

 4、测试

import tensorflow as tf
sess = tf.Session()
a = tf.constant(2)
b = tf.constant(3)
print(sess.run(a+b))

用百度 AI Studio完成猫狗识别-冯金伟博客园

 三、完成实验

1、源代码

import matplotlib
import tensorflow as tf
import keras
from keras import layers
import numpy as np
import os
import shutil

base_dir = './data/cat_dog'
train_dir = base_dir + '/train'
train_dog_dir = train_dir + '/dog'
train_cat_dir = train_dir + '/cat'
test_dir = base_dir + '/test'
test_dog_dir = test_dir + '/dog'
test_cat_dir = test_dir + '/cat'
dc_dir = './train'

if not os.path.exists(base_dir):

    os.mkdir(base_dir)

    os.mkdir(train_dir)
    os.mkdir(train_dog_dir)
    os.mkdir(train_cat_dir)
    os.mkdir(test_dir)
    os.mkdir(test_dog_dir)
    os.mkdir(test_cat_dir)

    fnames = ['cat.{}.jpg'.format(i) for i in range(1000)]
    for fname in fnames:
        src = os.path.join(dc_dir, fname)
        dst = os.path.join(train_cat_dir, fname)
        shutil.copyfile(src, dst)

    fnames = ['cat.{}.jpg'.format(i) for i in range(1000, 1500)]
    for fname in fnames:
        src = os.path.join(dc_dir, fname)
        dst = os.path.join(test_cat_dir, fname)
        shutil.copyfile(src, dst)

    fnames = ['dog.{}.jpg'.format(i) for i in range(1000)]
    for fname in fnames:
        src = os.path.join(dc_dir, fname)
        dst = os.path.join(train_dog_dir, fname)
        shutil.copyfile(src, dst)

    fnames = ['dog.{}.jpg'.format(i) for i in range(1000, 1500)]
    for fname in fnames:
        src = os.path.join(dc_dir, fname)
        dst = os.path.join(test_dog_dir, fname)
        shutil.copyfile(src, dst)

from keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale=1./255)
test_datagen = ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(
    train_dir,
    target_size=(200, 200),
    batch_size=20,
    class_mode='binary'
)

test_generator = test_datagen.flow_from_directory(
    test_dir,
    target_size=(200, 200),
    batch_size=20,
    class_mode='binary'
)

covn_base = keras.applications.VGG16(weights=None, include_top=False)
covn_base.summary()

model = keras.Sequential()
model.add(covn_base)
model.add(layers.GlobalAveragePooling2D())
model.add(layers.Dense(512, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))

covn_base.trainable = False #设置权重不可变,卷积基不可变

model.summary()

model.compile(optimizer=keras.optimizers.Adam(lr=0.001),
              loss='binary_crossentropy',
              metrics=['acc'])

history = model.fit_generator(
    train_generator,
    steps_per_epoch=100,
    epochs=15,
    validation_data=test_generator,
    validation_steps=50)

import matplotlib.pyplot as plt

plt.plot(history.epoch, history.history['loss'], 'r', label='loss')
plt.plot(history.epoch, history.history['val_loss'], 'b--', label='val_loss')
plt.legend()

plt.show()

plt.plot(history.epoch, history.history['acc'], 'r')
plt.plot(history.epoch, history.history['val_acc'], 'b--')

plt.show()

View Code

2、结果发现用免费的还是比较慢 ̄へ ̄  ̄へ ̄

用百度 AI Studio完成猫狗识别-冯金伟博客园

 3、新发现

完善个人资料可以获得算力使用高级版就很快了( ̄▽ ̄)~*

用百度 AI Studio完成猫狗识别-冯金伟博客园