Beispiel #1
0
    def __init__(self, cfg, train=True):
        super(ImageNet, self).__init__()
        self.cfg = cfg
        self.train = train

        self.data_infor = get_data(cfg.Data.dataset)
        self.traindir = os.path.join(cfg.Data.Dir, 'train')
        self.valdir = os.path.join(cfg.Data.Dir, 'val')
        self.catedict = dict(
            zip(sorted(os.listdir(self.valdir)[:1000]), range(1000)))

        # transform
        # assured inumpyut is CHW
        self.normalize = Normalize(mean=self.data_infor.mean,
                                   std=self.data_infor.std,
                                   data_format='CHW')
        self.transform_train = [
            RandomResizedCrop(cfg.Trans.crop_size,
                              scale=(cfg.Trans.min_area_ratio, 1.0),
                              ratio=(3. / 4, cfg.Trans.aspect_ratio))
        ]

        if self.data_infor.eigval is not None and self.data_infor.eigvec is not None \
                and cfg.Trans.random_color:
            lighting = Lighting(0.1, self.data_infor.eigval,
                                self.data_infor.eigvec)
            jitter = ColorJitter(0.4, 0.4, 0.4)
            self.transform_train.extend([jitter, lighting])
        self.transform_train.extend(
            [RandomHorizontalFlip(),
             ToTensor(), self.normalize])
        self.transform_train = Compose(self.transform_train)
        self.transform_val = Compose([
            Resize(cfg.Trans.scale_size),
            CenterCrop(cfg.Trans.crop_size),
            ToTensor(), self.normalize
        ])

        self.file_list = self.get_samples()
Beispiel #2
0
def main():

    print("\n Paddlepaddle version: {}\n".format(paddle.__version__))

    args = parse_args()

    # 初始化并行环境
    # dist.init_parallel_env()

    # 加载数据集
    train_dataset = paddle.vision.datasets.MNIST(mode='train',
                                                 transform=ToTensor())
    val_dataset = paddle.vision.datasets.MNIST(mode='test',
                                               transform=ToTensor())

    train_loader = paddle.io.DataLoader(train_dataset,
                                        batch_size=args.batch_size,
                                        shuffle=True)
    test_loader = paddle.io.DataLoader(val_dataset, batch_size=args.batch_size)

    # 模型搭建
    mnist = Mnist()
    paddle.summary(net=mnist, input_size=(-1, 1, 28, 28))
    # 增加paddle.DataParallel封装
    # mnist = paddle.DataParallel(mnist)

    optim = paddle.optimizer.Adam(parameters=mnist.parameters())
    loss_fn = paddle.nn.CrossEntropyLoss()

    start_epoch = 0
    epochs = args.epochs

    if args.resume:
        # Load checkpoint.
        print('==> Resuming from checkpoint..')
        info = np.load('./weights/info.npy', allow_pickle=True).item()
        start_epoch = info['epoch'] + 1
        val_loss = info['loss']
        val_acc = info['acc']
        print('Epoch {}, validation loss is: {loss:.4f}, validation accuracy is {acc:.4f}\n'\
            .format(start_epoch,loss=val_loss,acc=val_acc))
        mnist_state_dict = paddle.load('./weights/mnist.pdparams')
        mnist.set_state_dict(mnist_state_dict)
        optim_state_dict = paddle.load('./weights/optim.pdopt')
        optim.set_state_dict(optim_state_dict)

    best_acc = 0.0
    for epoch in range(start_epoch, epochs):
        # 训练
        mnist.train()
        loader = tqdm.tqdm(train_loader)
        for batch_id, (image, label) in enumerate(loader):
            predicts = mnist(image)
            loss = loss_fn(predicts, label)
            acc = paddle.metric.accuracy(predicts, label)
            loss.backward()
            optim.step()
            optim.clear_grad()
            description = (
                'Epoch {} (loss: {loss:.4f}, acc: {acc:.4f})'.format(
                    epoch, loss=loss.numpy().item(), acc=acc.numpy().item()))
            loader.set_description(description)

        # 测试
        mnist.eval()
        losses = 0.0
        accuracy = 0.0
        count = 0
        for batch_id, (image, label) in enumerate(test_loader):
            predicts = mnist(image)
            loss = loss_fn(predicts, label)
            acc = paddle.metric.accuracy(predicts, label)
            count += 1
            losses += loss.numpy().item()
            accuracy += acc.numpy().item()
        val_loss = losses / count
        val_acc = accuracy / count
        print("Testing: loss:{loss:.4f}, acc: {acc:.4f}".format(loss=val_loss,
                                                                acc=val_acc))

        # 保存测试过程结果
        result = OrderedDict()
        result['timestamp'] = datetime.now()
        result['epoch'] = epoch
        result['loss'] = val_loss
        result['accuracy'] = val_acc

        result_dir = './result/'
        if not os.path.exists(result_dir) and result_dir != '':
            os.makedirs(result_dir)
        result_file = os.path.join(result_dir, 'valid_results.csv')
        write_heading = not os.path.exists(result_file)
        with open(result_file, mode='a') as out:
            if write_heading:
                out.write(",".join([str(k) for k, v in result.items()]) + '\n')
            out.write(",".join([str(v) for k, v in result.items()]) + '\n')

        # 保存参数
        print('Saving checkpoint..')
        state = {'epoch': epoch, 'loss': val_loss, 'acc': val_acc}
        # 目前仅支持存储 Layer 或者 Optimizer 的 state_dict 。
        np.save('./weights/info.npy', state, allow_pickle=True)  # 保存相关参数
        paddle.save(mnist.state_dict(), './weights/mnist.pdparams')
        paddle.save(optim.state_dict(), './weights/optim.pdopt')

        # 保存用于部署的模型和参数
        if val_acc > best_acc:
            best_acc = val_acc
            paddle.jit.save(
                mnist,
                './deploy/mnist',
                input_spec=[InputSpec(shape=[1, 1, 28, 28], dtype='float32')])
    print(channel_list)

    resnet20 = ResNet20(100, channel_list)
    model = paddle.Model(resnet20)
    model.prepare(paddle.optimizer.Adam(parameters=model.parameters()),
                  paddle.nn.CrossEntropyLoss(),
                  paddle.metric.Accuracy())
    data_file = './data/cifar-100-python.tar.gz'
    transforms = paddle.vision.transforms.Compose([
        RandomHorizontalFlip(),
        RandomResizedCrop((32, 32)),
        SaturationTransform(0.2),
        BrightnessTransform(0.2), 
        ContrastTransform(0.2),
        HueTransform(0.2),
        ToTensor()
    ])
    train_dataset = paddle.vision.datasets.Cifar100(
        data_file, mode='train', transform=transforms)
    test_dataset = paddle.vision.datasets.Cifar100(
        data_file, mode='test', transform=ToTensor())
    model.fit(train_dataset, test_dataset, epochs=100, batch_size=64, verbose=0)

    result = model.evaluate(test_dataset, batch_size=64)
    print(ii, '\t', channel_list, '\t', result['acc'])

    json_result["arch%d" % ii] = {
        "arc": channel_list,
        "acc": result["acc"]
    }
import paddle
import paddle.nn.functional as F
from paddle.vision.transforms import ToTensor
import numpy as np
import matplotlib.pyplot as plt

print(paddle.__version__)
transform = ToTensor()
cifar10_train = paddle.vision.datasets.Cifar10(mode='train',
                                               transform=transform)
cifar10_test = paddle.vision.datasets.Cifar10(mode='test', transform=transform)


class MyNet(paddle.nn.Layer):
    def __init__(self, num_classes=1):
        super(MyNet, self).__init__()

        self.conv1 = paddle.nn.Conv2D(in_channels=3,
                                      out_channels=32,
                                      kernel_size=(3, 3))
        self.pool1 = paddle.nn.MaxPool2D(kernel_size=2, stride=2)

        self.conv2 = paddle.nn.Conv2D(in_channels=32,
                                      out_channels=64,
                                      kernel_size=(3, 3))
        self.pool2 = paddle.nn.MaxPool2D(kernel_size=2, stride=2)

        self.conv3 = paddle.nn.Conv2D(in_channels=64,
                                      out_channels=64,
                                      kernel_size=(3, 3))
Beispiel #5
0
def inference(model, left_imgs, right_ims, LOG):

    stages = 4
    model.eval()

    transform = Compose([ToTensor(),
                         Normalize(mean=imagenet_stats["mean"],
                                   std=imagenet_stats["std"])])

    for index in range(len(left_imgs)):
        # LOG.info("left = {}\tright = {}".format(left_imgs[index], right_ims[index]))

        left_img = cv2.imread(left_imgs[index], cv2.IMREAD_UNCHANGED)
        right_img = cv2.imread(right_ims[index], cv2.IMREAD_UNCHANGED)

        h, w, c = left_img.shape
        th, tw = 368, 1232

        if h<th or w<tw:
            continue

        left_img = left_img[h - th:h, w - tw:w, :]
        right_img = right_img[h - th:h, w - tw:w, :]

        left_input = transform(left_img[:, :, ::-1]).unsqueeze(axis=0)
        right_input = transform(right_img[:, :, ::-1]).unsqueeze(axis=0)

        with paddle.no_grad():

            start_time = time.time()
            outputs = model(left_input, right_input)
            cost_time = time.time()-start_time

            ss = "Inference 4 stages cost = {:.3f} sec, FPS = {:.1f}".format(cost_time, 1/cost_time)

            for stage in range(stages):
                outputs[stage] = outputs[stage].squeeze(axis=[0, 1]).numpy().astype(np.uint8)
                color_disp = cv2.applyColorMap(cv2.convertScaleAbs(outputs[stage], alpha=1, beta=0), cv2.COLORMAP_JET)

                if args.left_img:
                    temp_path = args.left_img.split("/")[0:-1]
                    temp_path = "/".join(temp_path)
                    save_img_path = os.path.join(temp_path, str(stage+1)+".png")
                    cv2.imwrite(save_img_path, color_disp)
                    LOG.info("{}\t\tSave img = {}".format(ss, save_img_path))

            if args.vis:
                concat_img = np.concatenate((left_img, color_disp), axis=0)
                # cv2.imshow("left_img", left_img)
                # cv2.imshow("raw_disp", outputs[stage])
                # cv2.imshow("color_disp", color_disp)
                cv2.imshow("concat_img", concat_img)
                key = cv2.waitKey(0)
                if key == ord("q"):
                    break

            if not args.left_img:
                img_name = left_imgs[index].split("/")[-1]
                save_img_path = os.path.join(args.save_path, img_name)
                cv2.imwrite(save_img_path, color_disp)
                LOG.info("{}\t\tSave img = {}".format(ss, save_img_path))
Beispiel #6
0
"""
 手写数字识别

学习文档地址:https://www.paddlepaddle.org.cn/documentation/docs/zh/guides/02_paddle2.0_develop/01_quick_start_cn.html

深度学习任务一般分为几个核心步骤:
 1.数据集的准备和加载;
 2.模型构建;
 3.模型训练;
 4.模型评估。


"""

train_dataset = paddle.vision.datasets.MNIST(mode='train',
                                             transform=ToTensor())
val_dataset = paddle.vision.datasets.MNIST(mode='test', transform=ToTensor())

print(train_dataset.mode)
print(train_dataset.transform)

mnist = paddle.nn.Sequential(paddle.nn.Flatten(), paddle.nn.Linear(784, 512),
                             paddle.nn.ReLU(), paddle.nn.Dropout(0.2),
                             paddle.nn.Linear(512, 10))

# 预计模型结构生成模型对象,便于进行后续的配置、训练和验证
model = paddle.Model(mnist)

# 模型训练相关配置,准备损失计算方法,优化器和精度计算方法
model.prepare(paddle.optimizer.Adam(parameters=model.parameters()),
              paddle.nn.CrossEntropyLoss(), paddle.metric.Accuracy())
Beispiel #7
0
import paddle
from paddle import nn
from paddle import optimizer
from paddle.vision.transforms import Compose, Normalize
from paddle.vision.transforms import ToTensor

import paddle.distributed as dist

from model_zoo import Model

transform_tuple = Compose([ToTensor(), Normalize()])

parallel_flag = False

if __name__ == '__main__':

    if parallel_flag:
        dist.init_parallel_env()

    leakRelu_crossEntropy_adam = Model(transform_tuple, nn.LeakyReLU,
                                       nn.CrossEntropyLoss, optimizer.Adam)

    leakRelu_crossEntropy_adam.train()
    leakRelu_crossEntropy_adam.validate()

    relu_crossEntropy_sgd = Model(transform_tuple, nn.ReLU,
                                  nn.CrossEntropyLoss, optimizer.SGD)
    relu_crossEntropy_sgd.train()
    relu_crossEntropy_sgd.validate()

    leakReLuCrossEntropySgd = Model(transform_tuple, nn.LeakyReLU,
Beispiel #8
0
# Author: Acer Zhang
# Datetime: 2021/2/25
# Copyright belongs to the author.
# Please indicate the source for reprinting.

import paddle

from paddle.vision.transforms import Compose, Resize, ToTensor
from paddle.vision.models import resnet50
from paddle.vision.datasets import Cifar100

# 导入RIFLE模块
from paddle_rifle.rifle import RIFLECallback

# 定义数据预处理
transform = Compose([Resize(224), ToTensor()])

# 加载Cifar100数据集
train_data = Cifar100(transform=transform)
test_data = Cifar100(mode="test", transform=transform)

# 加载Resnet50
net = resnet50(True, num_classes=100)
# 获取Resnet50的输出层
fc_layer = net.fc
"""
# 自定义网络场景下的输出层获取示例

class Net(paddle.nn.Layer):
    def __init__(self):
        super(Net, self).__init__()
Beispiel #9
0
URL = {
    "RN50": "https://bj.bcebos.com/paddleaudio/examples/clip/RN50.pdparams",
    "RN101": "https://bj.bcebos.com/paddleaudio/examples/clip/RN101.pdparams",
    "VIT": "https://bj.bcebos.com/paddleaudio/examples/clip/ViT-B-32.pdparam",
}

MEAN, STD = (0.48145466, 0.4578275, 0.40821073), (0.26862954, 0.26130258,
                                                  0.27577711)
_tokenizer = SimpleTokenizer()

transform = Compose([
    Resize(224, interpolation="bicubic"),
    CenterCrop(224),
    lambda image: image.convert("RGB"),
    ToTensor(),
    Normalize(mean=MEAN, std=STD),
    lambda t: t.unsqueeze_(0),
])


def tokenize(texts: Union[str, List[str]], context_length: int = 77):
    """
    Returns the tokenized representation of given input string(s)
    Parameters
    ----------
    texts : Union[str, List[str]]
        An input string or a list of input strings to tokenize
    context_length : int
        The context length to use; all CLIP models use 77 as the context length
    Returns