├── BasicMathematicsForArtificialIntelligence └── README.md ├── DataSet_conversion ├── COCO_extract_person_label.py ├── CrownHuman_dataset_convert.py ├── split_train_val.py ├── voc_format.py └── xml2json.py ├── DeepLearningObjectDetection ├── Keras-SSD │ ├── README.md │ ├── VOCdevkit │ │ └── VOC2007 │ │ │ ├── Annotations │ │ │ └── README.md │ │ │ ├── ImageSets │ │ │ └── Main │ │ │ │ └── README.md │ │ │ ├── JPEGImages │ │ │ └── README.md │ │ │ └── voc2ssd.py │ ├── VisionForPriorBox.py │ ├── __pycache__ │ │ └── ssd.cpython-36.pyc │ ├── img │ │ ├── 000005.jpg │ │ ├── 000007.jpg │ │ ├── 000009.jpg │ │ └── 000012.jpg │ ├── logs │ │ └── README.md │ ├── model_data │ │ ├── README.md │ │ ├── prior_boxes_ssd300.pkl │ │ ├── simhei.ttf │ │ └── voc_classes.txt │ ├── nets │ │ ├── VGG16.py │ │ ├── __pycache__ │ │ │ ├── VGG16.cpython-36.pyc │ │ │ ├── ssd.cpython-36.pyc │ │ │ └── ssd_layers.cpython-36.pyc │ │ ├── ssd.py │ │ ├── ssd_layers.py │ │ └── ssd_training.py │ ├── predict.py │ ├── ssd.py │ ├── test.py │ ├── train.py │ ├── utils │ │ ├── __pycache__ │ │ │ └── utils.cpython-36.pyc │ │ └── utils.py │ └── voc_annotation.py ├── PyTorch-YOLOv3 │ ├── README.md │ ├── assets │ │ ├── dog.png │ │ ├── giraffe.png │ │ ├── messi.png │ │ └── traffic.png │ ├── config │ │ ├── coco.data │ │ ├── create_custom_model.sh │ │ ├── custom.data │ │ ├── yolov3-tiny.cfg │ │ └── yolov3.cfg │ ├── data │ │ ├── coco.names │ │ ├── custom │ │ │ ├── classes.names │ │ │ ├── images │ │ │ │ └── train.jpg │ │ │ ├── labels │ │ │ │ └── train.txt │ │ │ ├── train.txt │ │ │ └── valid.txt │ │ ├── get_coco_dataset.sh │ │ ├── samples │ │ │ ├── dog.jpg │ │ │ ├── eagle.jpg │ │ │ ├── field.jpg │ │ │ ├── giraffe.jpg │ │ │ ├── herd_of_horses.jpg │ │ │ ├── messi.jpg │ │ │ ├── person.jpg │ │ │ ├── room.jpg │ │ │ └── street.jpg │ │ ├── train.txt │ │ └── valid.txt │ ├── detect.py │ ├── models.py │ ├── requirements.txt │ ├── test.py │ ├── train.py │ └── utils │ │ ├── augmentations.py │ │ ├── datasets.py │ │ ├── logger.py │ │ ├── parse_config.py │ │ └── utils.py └── README.md ├── DeepLearningPapersTranslation └── README.md ├── KerasLearningNote ├── Keras-my-train-model │ ├── README.md │ ├── custom_layers.py │ ├── densenetloss121_1_1.jpg │ ├── densenetloss121_1_3.jpg │ ├── densenetloss121_2_1.jpg │ ├── densenetloss121_2_3.jpg │ ├── densenetloss_1_1.jpg │ ├── densenetloss_1_3.jpg │ ├── densenetloss_2_1.jpg │ ├── densenetloss_2_3.jpg │ ├── lenetloss_1_1.jpg │ ├── lenetloss_1_3.jpg │ ├── lenetloss_2_1.jpg │ ├── lenetloss_2_3.jpg │ ├── myalexnet.py │ ├── mycode_AlexNet.py │ ├── mycode_DenseNet.py │ ├── mycode_LeNet.py │ ├── mycode_ResNet.py │ ├── mycode_VGGNet.py │ ├── mycode_ZFNet.py │ ├── mydensenet.py │ ├── mydensenet121.py │ ├── mylenet.py │ ├── myresnet50.py │ ├── myvggnet.py │ ├── myzfnet.py │ ├── resnetloss_1_1.jpg │ ├── resnetloss_1_3.jpg │ ├── resnetloss_2_1.jpg │ ├── resnetloss_2_3.jpg │ ├── vggnetloss_1_1.jpg │ ├── vggnetloss_1_3.jpg │ ├── vggnetloss_2_1.jpg │ ├── vggnetloss_2_3.jpg │ ├── zfnetloss_1_1.jpg │ ├── zfnetloss_1_3.jpg │ ├── zfnetloss_2_1.jpg │ └── zfnetloss_2_3.jpg ├── KerasCode_1 │ ├── Keras_h5topb.py │ ├── Keras_h5totflile.py │ ├── UsingPbPredict.py │ └── keras_parctice1.py ├── KerasCode_2 │ ├── Keras_cifar10.py │ ├── keras_CNNparctice1.py │ ├── keras_CNNparctice2.py │ ├── keras_DataEnhancement.py │ └── keras_PredictModel.py ├── KerasCode_3 │ ├── keras_ExtractBottleneckFeature.py │ ├── keras_FineTuningCompleteCode.py │ ├── keras_SaveBottleneckFeature.py │ ├── keras_funeTuning.py │ ├── keras_parcticeCompleteCode.py │ └── keras_parcticeCompleteCode1.py ├── KerasCode_4 │ ├── Keras_VGG16.py │ ├── keras_FinetuneInceptionV3.py │ ├── keras_MusicTaggerCRNNcode.py │ ├── keras_ResNet50Application.py │ ├── keras_VGG16Application.py │ ├── keras_VGG19Application.py │ └── keras_parcticeCompleteCode.py ├── KerasCode_5 │ ├── keras_parcticeCNN1.py │ ├── keras_parcticeCNN2.py │ ├── keras_parcticeCNN3.py │ ├── keras_parcticeLSTM.py │ └── keras_recordLossHistory.py ├── README.md ├── traindata10code │ ├── README.md │ ├── cifar10_trainrecord.jpg │ ├── custom_layers.py │ ├── data_10record.txt │ ├── densenet121loss_1_3.jpg │ ├── densenet121loss_2_3.jpg │ ├── densenetloss_1_3.jpg │ ├── densenetloss_2_3.jpg │ ├── lenetloss_1_3.jpg │ ├── lenetloss_2_3.jpg │ ├── mydensenet121.py │ ├── mymodel.py │ ├── resnetloss_1_3.jpg │ ├── resnetloss_2_3.jpg │ ├── trainCifar10data.py │ ├── vggnetloss_1_3.jpg │ └── vggnetloss_2_3.jpg ├── traindata10code_plus │ ├── README.md │ ├── custom_layers.py │ ├── data_10record.txt │ ├── densenetloss121_1.jpg │ ├── densenetloss121_2.jpg │ ├── densenetloss1_3.jpg │ ├── densenetloss2_3.jpg │ ├── lenetloss1_3.jpg │ ├── lenetloss2_3.jpg │ ├── mydensenet121.py │ ├── mymodel.py │ ├── resnetloss1_3.jpg │ ├── resnetloss2_3.jpg │ ├── trainCifar10data.py │ ├── train_record.jpg │ ├── traindata.py │ ├── vggnetloss1_3.jpg │ └── vggnetloss2_3.jpg └── traindata5code │ ├── README.md │ ├── custom_layers.py │ ├── data5_trainrecord.jpg │ ├── data5plus_trainrecord.jpg │ ├── data_5record.txt │ ├── mydensenet121.py │ ├── mymodel.py │ └── trainmydata.py ├── PyTorchLearningNote ├── PyTorchCode_1 │ ├── pytorch_practice1.py │ └── pytorch_practice2.py ├── PyTorchCode_2 │ ├── pytorch_SetUpModel.py │ ├── pytorch_practice1.py │ ├── pytorch_practiceByOrderDict.py │ ├── pytorch_practiceCompleteCode.py │ └── pytorch_practiceCompleteCode2.py ├── PyTorchCode_3 │ ├── pytorch_practiceCompleteCode1.py │ ├── pytorch_practiceCompleteCode2.py │ └── pytorch_practiceCompleteCode3.py └── README.md ├── README.md └── TensorflowLearningNote ├── AlexNet_code_folder └── myalexnet.py ├── DenseNet_code_folder ├── custom_layers.py ├── mydensenet.py ├── mydensenet1.py ├── mydensenet121.py ├── mydensenet161.py ├── mydensenet169.py └── train_model.py ├── GoogLeNet_code_folder └── mygooglenet.py ├── LeNet-5_code_folder ├── mnist2image.py ├── mnist2tfrecord.py ├── mylenet_trainminist.py ├── parsedata.py ├── readtfrecord.py ├── tf_lenet5.py └── train_lenet_with_tfrecord.py ├── MobileNet_code_folder ├── depthwise_conv2d.py ├── mymobilenetv1.py └── mymobilenetv2.py ├── README.md ├── ResNet_code_folder ├── mycode_ResNet.py ├── myresnet50.py ├── resnet50_2.py └── resnet50_wu.py ├── SENet_code_folder ├── SE_Inception.py ├── SE_ResNeXt.py └── train_cifar10.py ├── SiameseNet_code_folder ├── keras_siamese_Omniglot.py ├── keras_siamese_mnist.py └── keras_siamese_predict.py └── VGGNet_code_folder └── myvggnet.py /BasicMathematicsForArtificialIntelligence/README.md: -------------------------------------------------------------------------------- 1 | # 人工智能必备数学基础地址 2 | *** 3 | ###### 摘要 4 | > 这个文件夹存放的是我整理了一些数学基础的博客地址,希望可以帮助到别人 5 | *** 6 | ### 人工智能必备数学基础:高等数学基础(1) 7 | - 地址:https://www.cnblogs.com/wj-1314/p/14070109.html 8 | *** 9 | ### 人工智能必备数学基础:高等数学基础(2) 10 | - 地址:https://www.cnblogs.com/wj-1314/p/14086644.html 11 | *** 12 | ### 人工智能必备数学基础:高等数学基础(3) 13 | - 地址:https://www.cnblogs.com/wj-1314/p/14155556.html 14 | *** 15 | ### 人工智能必备数学基础:线性代数基础(1) 16 | - 地址:https://www.cnblogs.com/wj-1314/p/14081157.html 17 | *** 18 | ### 人工智能必备数学基础:线性代数基础(2) 19 | - 地址:https://www.cnblogs.com/wj-1314/p/14085421.html 20 | *** 21 | ### 人工智能必备数学基础:概率论与数理统计(1) 22 | - 地址:https://www.cnblogs.com/wj-1314/p/14085772.html 23 | *** 24 | ### 人工智能必备数学基础:概率论与数理统计(2) 25 | - 地址:https://www.cnblogs.com/wj-1314/p/9034681.html 26 | -------------------------------------------------------------------------------- /DataSet_conversion/COCO_extract_person_label.py: -------------------------------------------------------------------------------- 1 | import json 2 | import os 3 | import shutil 4 | import skimage.io as io 5 | import cv2 6 | from tqdm import tqdm 7 | import matplotlib.pyplot as plt 8 | from pycocotools.coco import COCO 9 | from PIL import Image, ImageDraw 10 | 11 | from voc_format import headstr, tailstr, objstr 12 | 13 | 14 | def mkr(path): 15 | # 检查目录是否存在,如果存在,先删除再创建,否则,直接创建 16 | if not os.path.exists(path): 17 | # 可以创建多级目录 18 | os.makedirs(path) 19 | 20 | 21 | def id2name(coco): 22 | classes = dict() 23 | for cls in coco.dataset['categories']: 24 | classes[cls['id']] = cls['name'] 25 | return classes 26 | 27 | 28 | def write_xml(anno_path, head, objs, tail): 29 | f = open(anno_path, "w") 30 | f.write(head) 31 | for obj in objs: 32 | f.write(objstr % (obj[0], obj[1], obj[2], obj[3], obj[4])) 33 | f.write(tail) 34 | 35 | 36 | def save_annotations_and_imgs(coco, image_dir, save_image_path, save_label_path, filename, objs): 37 | # 将图片转为xml,例:COCO_train2017_000000196610.jpg-->COCO_train2017_000000196610.xml 38 | mkr(save_label_path) 39 | anno_path = save_label_path + '/' + filename[:-3]+'xml' 40 | img_path = image_dir + '/'+filename 41 | 42 | mkr(save_image_path) 43 | dst_imgpath = save_image_path + '/' + filename 44 | 45 | img = cv2.imread(img_path) 46 | # if (img.shape[2] == 1): 47 | # print(filename + " not a RGB image") 48 | # return 49 | shutil.copy(img_path, dst_imgpath) 50 | 51 | head = headstr % (filename, img.shape[1], img.shape[0], img.shape[2]) 52 | tail = tailstr 53 | write_xml(anno_path, head, objs, tail) 54 | 55 | 56 | def showimg(coco, dataset, img, classes, cls_id, show=False): 57 | I = Image.open('%s/%s' % (dataset, img['file_name'])) 58 | # 通过id,得到注释的信息 59 | annIds = coco.getAnnIds(imgIds=img['id'], catIds=cls_id, iscrowd=None) 60 | # print(annIds) 61 | anns = coco.loadAnns(annIds) 62 | # print(anns) 63 | # coco.showAnns(anns) 64 | objs = [] 65 | for ann in anns: 66 | class_name = classes[ann['category_id']] 67 | if class_name in classes_names: 68 | # print(class_name) 69 | if 'bbox' in ann: 70 | bbox = ann['bbox'] 71 | xmin = int(bbox[0]) 72 | ymin = int(bbox[1]) 73 | xmax = int(bbox[2] + bbox[0]) 74 | ymax = int(bbox[3] + bbox[1]) 75 | obj = [class_name, xmin, ymin, xmax, ymax] 76 | objs.append(obj) 77 | draw = ImageDraw.Draw(I) 78 | draw.rectangle([xmin, ymin, xmax, ymax]) 79 | if show: 80 | plt.figure() 81 | plt.axis('off') 82 | plt.imshow(I) 83 | plt.show() 84 | 85 | return objs 86 | 87 | 88 | if __name__ == '__main__': 89 | # json file name 90 | jsonfilename = '/datasets/COCO2017_val/JSON_folder/instances_train2017.json' 91 | # image_dir 92 | image_dir = '/datasets/COCO2017_tovoc/train2017/' 93 | # save path; label format is VOC format(.xml) 94 | save_image_path = '/datasets/COCO2017_train/person_image/' 95 | save_label_path = 'datasets/COCO2017_train/annotations/' 96 | # 提取COCO 数据集中特定的类别, 这里全部提取人 97 | classes_names = ['person'] 98 | 99 | # 使用COCO API用来初始化注释数据 100 | coco = COCO(jsonfilename) 101 | 102 | # 获取COCO数据集中的所有类别 103 | classes = id2name(coco) 104 | #[1, 2, 3, 4, 6, 8] 105 | classes_ids = coco.getCatIds(catNms=classes_names) 106 | for cls in classes_names: 107 | # 获取该类的id 108 | cls_id = coco.getCatIds(catNms=[cls]) 109 | img_ids = coco.getImgIds(catIds=cls_id) 110 | # print(cls, len(img_ids)) 111 | # imgIds=img_ids[0:10] 112 | for imgId in tqdm(img_ids): 113 | img = coco.loadImgs(imgId)[0] 114 | filename = img['file_name'] 115 | # print(filename) 116 | objs = showimg(coco, image_dir, img, classes, classes_ids, show=False) 117 | print(objs) 118 | save_annotations_and_imgs(coco, image_dir, save_image_path, 119 | save_label_path, filename, objs) 120 | -------------------------------------------------------------------------------- /DataSet_conversion/split_train_val.py: -------------------------------------------------------------------------------- 1 | import os 2 | import random 3 | 4 | root_dir = '/home/ilc/Desktop/workdata/YOLO_Series/datasets/VOC_COCOval_person/' 5 | 6 | # test 0,trainval 1,train 0.7, val 0.3 7 | trainval_percent = 1 8 | train_percent = 0.7 9 | xmlfilepath = root_dir+'Annotations' 10 | txtsavepath = root_dir+'ImageSets/Main' 11 | total_xml = os.listdir(xmlfilepath) 12 | 13 | num = len(total_xml) # 100 14 | list = range(num) 15 | tv = int(num*trainval_percent) # 80 16 | tr = int(tv*train_percent) # 80*0.7=56 17 | trainval = random.sample(list, tv) 18 | train = random.sample(trainval, tr) 19 | 20 | ftrainval = open(root_dir+'ImageSets/Main/trainval.txt', 'w') 21 | ftest = open(root_dir+'ImageSets/Main/test.txt', 'w') 22 | ftrain = open(root_dir+'ImageSets/Main/train.txt', 'w') 23 | fval = open(root_dir+'ImageSets/Main/val.txt', 'w') 24 | 25 | for i in list: 26 | name = total_xml[i][:-4]+'\n' 27 | if i in trainval: 28 | ftrainval.write(name) 29 | if i in train: 30 | ftrain.write(name) 31 | else: 32 | fval.write(name) 33 | else: 34 | ftest.write(name) 35 | 36 | ftrainval.close() 37 | ftrain.close() 38 | fval.close() 39 | ftest .close() 40 | -------------------------------------------------------------------------------- /DataSet_conversion/voc_format.py: -------------------------------------------------------------------------------- 1 | headstr = """\ 2 | 3 | VOC 4 | %s 5 | 6 | My Database 7 | COCO 8 | flickr 9 | NULL 10 | 11 | 12 | NULL 13 | company 14 | 15 | 16 | %d 17 | %d 18 | %d 19 | 20 | 0 21 | """ 22 | 23 | objstr = """\ 24 | 25 | %s 26 | Unspecified 27 | 0 28 | 0 29 | 30 | %d 31 | %d 32 | %d 33 | %d 34 | 35 | 36 | """ 37 | 38 | tailstr = '''\ 39 | 40 | ''' 41 | -------------------------------------------------------------------------------- /DeepLearningObjectDetection/Keras-SSD/README.md: -------------------------------------------------------------------------------- 1 | # ssd-keras 2 | 这是一个ssd-keras的源码,可以用于训练自己的模型。 3 | 4 | # 文件下载 5 | 训练所需的ssd_weights.h5可以在百度云下载。 6 | 链接: https://pan.baidu.com/s/17diCwawNy9WcqXhddl8qIw 7 | 提取码: kquc 8 | # 训练步骤 9 | 1、本文使用VOC格式进行训练。 10 | 2、训练前将标签文件放在VOCdevkit文件夹下的VOC2007文件夹下的Annotation中。 11 | 3、训练前将图片文件放在VOCdevkit文件夹下的VOC2007文件夹下的JPEGImages中。 12 | 4、在训练前利用voc2ssd.py文件生成对应的txt。 13 | 5、再运行根目录下的voc_annotation.py,运行前需要将classes改成你自己的classes。 14 | ```python 15 | classes = ["aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"] 16 | ``` 17 | 6、就会生成对应的2007_train.txt,每一行对应其图片位置及其真实框的位置。 18 | 7、在训练前需要修改model_data里面的voc_classes.txt文件,需要将classes改成你自己的classes。 19 | 8、修改train.py里面的NUM_CLASSES与需要训练的种类的个数相同。运行train.py即可开始训练。 20 | 21 | # Reference 22 | https://github.com/pierluigiferrari/ssd_keras 23 | https://github.com/kuhung/SSD_keras 24 | -------------------------------------------------------------------------------- /DeepLearningObjectDetection/Keras-SSD/VOCdevkit/VOC2007/Annotations/README.md: -------------------------------------------------------------------------------- 1 | 存放标签文件 -------------------------------------------------------------------------------- /DeepLearningObjectDetection/Keras-SSD/VOCdevkit/VOC2007/ImageSets/Main/README.md: -------------------------------------------------------------------------------- 1 | 存放训练索引文件 -------------------------------------------------------------------------------- /DeepLearningObjectDetection/Keras-SSD/VOCdevkit/VOC2007/JPEGImages/README.md: -------------------------------------------------------------------------------- 1 | 存放图片文件 -------------------------------------------------------------------------------- /DeepLearningObjectDetection/Keras-SSD/VOCdevkit/VOC2007/voc2ssd.py: -------------------------------------------------------------------------------- 1 | import os 2 | import random 3 | 4 | xmlfilepath=r'./VOCdevkit/VOC2007/Annotations' 5 | saveBasePath=r"./VOCdevkit/VOC2007/ImageSets/Main/" 6 | 7 | trainval_percent=1 8 | train_percent=1 9 | 10 | temp_xml = os.listdir(xmlfilepath) 11 | total_xml = [] 12 | for xml in temp_xml: 13 | if xml.endswith(".xml"): 14 | total_xml.append(xml) 15 | 16 | num=len(total_xml) 17 | list=range(num) 18 | tv=int(num*trainval_percent) 19 | tr=int(tv*train_percent) 20 | trainval= random.sample(list,tv) 21 | train=random.sample(trainval,tr) 22 | 23 | print("train and val size",tv) 24 | print("traub suze",tr) 25 | ftrainval = open(os.path.join(saveBasePath,'trainval.txt'), 'w') 26 | ftest = open(os.path.join(saveBasePath,'test.txt'), 'w') 27 | ftrain = open(os.path.join(saveBasePath,'train.txt'), 'w') 28 | fval = open(os.path.join(saveBasePath,'val.txt'), 'w') 29 | 30 | for i in list: 31 | name=total_xml[i][:-4]+'\n' 32 | if i in trainval: 33 | ftrainval.write(name) 34 | if i in train: 35 | ftrain.write(name) 36 | else: 37 | fval.write(name) 38 | else: 39 | ftest.write(name) 40 | 41 | ftrainval.close() 42 | ftrain.close() 43 | fval.close() 44 | ftest .close() 45 | -------------------------------------------------------------------------------- /DeepLearningObjectDetection/Keras-SSD/__pycache__/ssd.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/DeepLearningObjectDetection/Keras-SSD/__pycache__/ssd.cpython-36.pyc -------------------------------------------------------------------------------- /DeepLearningObjectDetection/Keras-SSD/img/000005.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/DeepLearningObjectDetection/Keras-SSD/img/000005.jpg -------------------------------------------------------------------------------- /DeepLearningObjectDetection/Keras-SSD/img/000007.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/DeepLearningObjectDetection/Keras-SSD/img/000007.jpg -------------------------------------------------------------------------------- /DeepLearningObjectDetection/Keras-SSD/img/000009.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/DeepLearningObjectDetection/Keras-SSD/img/000009.jpg -------------------------------------------------------------------------------- /DeepLearningObjectDetection/Keras-SSD/img/000012.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/DeepLearningObjectDetection/Keras-SSD/img/000012.jpg -------------------------------------------------------------------------------- /DeepLearningObjectDetection/Keras-SSD/logs/README.md: -------------------------------------------------------------------------------- 1 | 存放训练后的模型 -------------------------------------------------------------------------------- /DeepLearningObjectDetection/Keras-SSD/model_data/README.md: -------------------------------------------------------------------------------- 1 | 其中:ssd_weights.h5需要自己下载 -------------------------------------------------------------------------------- /DeepLearningObjectDetection/Keras-SSD/model_data/prior_boxes_ssd300.pkl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/DeepLearningObjectDetection/Keras-SSD/model_data/prior_boxes_ssd300.pkl -------------------------------------------------------------------------------- /DeepLearningObjectDetection/Keras-SSD/model_data/simhei.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/DeepLearningObjectDetection/Keras-SSD/model_data/simhei.ttf -------------------------------------------------------------------------------- /DeepLearningObjectDetection/Keras-SSD/model_data/voc_classes.txt: -------------------------------------------------------------------------------- 1 | car 2 | ball -------------------------------------------------------------------------------- /DeepLearningObjectDetection/Keras-SSD/nets/__pycache__/VGG16.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/DeepLearningObjectDetection/Keras-SSD/nets/__pycache__/VGG16.cpython-36.pyc -------------------------------------------------------------------------------- /DeepLearningObjectDetection/Keras-SSD/nets/__pycache__/ssd.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/DeepLearningObjectDetection/Keras-SSD/nets/__pycache__/ssd.cpython-36.pyc -------------------------------------------------------------------------------- /DeepLearningObjectDetection/Keras-SSD/nets/__pycache__/ssd_layers.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/DeepLearningObjectDetection/Keras-SSD/nets/__pycache__/ssd_layers.cpython-36.pyc -------------------------------------------------------------------------------- /DeepLearningObjectDetection/Keras-SSD/predict.py: -------------------------------------------------------------------------------- 1 | from keras.layers import Input 2 | from ssd import SSD 3 | from PIL import Image 4 | 5 | ssd = SSD() 6 | 7 | while True: 8 | img = input('Input image filename:') 9 | try: 10 | image = Image.open(img) 11 | except: 12 | print('Open Error! Try again!') 13 | continue 14 | else: 15 | r_image = ssd.detect_image(image) 16 | r_image.show() 17 | ssd.close_session() 18 | -------------------------------------------------------------------------------- /DeepLearningObjectDetection/Keras-SSD/test.py: -------------------------------------------------------------------------------- 1 | from nets.ssd import SSD300 2 | 3 | NUM_CLASSES = 21 4 | input_shape = (300, 300, 3) 5 | model = SSD300(input_shape, num_classes=NUM_CLASSES) 6 | model.summary() 7 | # for i in range(len(model.layers)): 8 | # print(i,model.layers[i].name) -------------------------------------------------------------------------------- /DeepLearningObjectDetection/Keras-SSD/train.py: -------------------------------------------------------------------------------- 1 | from keras.backend.tensorflow_backend import set_session 2 | from keras.callbacks import TensorBoard, ModelCheckpoint, ReduceLROnPlateau, EarlyStopping 3 | from keras.models import Model 4 | from keras.preprocessing import image 5 | from nets.ssd import SSD300 6 | from nets.ssd_training import MultiboxLoss,Generator 7 | from utils.utils import BBoxUtility 8 | from keras.optimizers import Adam 9 | import matplotlib.pyplot as plt 10 | import numpy as np 11 | import pickle 12 | import tensorflow as tf 13 | import cv2 14 | import keras 15 | import os 16 | import sys 17 | if __name__ == "__main__": 18 | log_dir = "logs/" 19 | annotation_path = '2007_train.txt' 20 | 21 | NUM_CLASSES = 4 22 | input_shape = (300, 300, 3) 23 | priors = pickle.load(open('model_data/prior_boxes_ssd300.pkl', 'rb')) 24 | bbox_util = BBoxUtility(NUM_CLASSES, priors) 25 | 26 | # 0.1用于验证,0.9用于训练 27 | val_split = 0.1 28 | with open(annotation_path) as f: 29 | lines = f.readlines() 30 | np.random.seed(10101) 31 | np.random.shuffle(lines) 32 | np.random.seed(None) 33 | num_val = int(len(lines)*val_split) 34 | num_train = len(lines) - num_val 35 | 36 | model = SSD300(input_shape, num_classes=NUM_CLASSES) 37 | model.load_weights('model_data/ssd_weights.h5', by_name=True, skip_mismatch=True) 38 | 39 | # 训练参数设置 40 | logging = TensorBoard(log_dir=log_dir) 41 | checkpoint = ModelCheckpoint(log_dir + 'ep{epoch:03d}-loss{loss:.3f}-val_loss{val_loss:.3f}.h5', 42 | monitor='val_loss', save_weights_only=True, save_best_only=True, period=1) 43 | reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=2, verbose=1) 44 | early_stopping = EarlyStopping(monitor='val_loss', min_delta=0, patience=6, verbose=1) 45 | 46 | BATCH_SIZE = 4 47 | gen = Generator(bbox_util, BATCH_SIZE, lines[:num_train], lines[num_train:], 48 | (input_shape[0], input_shape[1]),NUM_CLASSES, do_crop=True) 49 | 50 | if True: 51 | model.compile(optimizer=Adam(lr=1e-4),loss=MultiboxLoss(NUM_CLASSES, neg_pos_ratio=5.0).compute_loss) 52 | model.fit_generator(gen.generate(True), 53 | steps_per_epoch=num_train//BATCH_SIZE, 54 | validation_data=gen.generate(False), 55 | validation_steps=num_val//BATCH_SIZE, 56 | epochs=5, 57 | initial_epoch=0, 58 | callbacks=[logging, checkpoint, reduce_lr, early_stopping]) 59 | 60 | if True: 61 | model.compile(optimizer=Adam(lr=5e-5),loss=MultiboxLoss(NUM_CLASSES, neg_pos_ratio=3.0).compute_loss) 62 | model.fit_generator(gen.generate(True), 63 | steps_per_epoch=num_train//BATCH_SIZE, 64 | validation_data=gen.generate(False), 65 | validation_steps=num_val//BATCH_SIZE, 66 | epochs=50, 67 | initial_epoch=5, 68 | callbacks=[logging, checkpoint, reduce_lr, early_stopping]) -------------------------------------------------------------------------------- /DeepLearningObjectDetection/Keras-SSD/utils/__pycache__/utils.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/DeepLearningObjectDetection/Keras-SSD/utils/__pycache__/utils.cpython-36.pyc -------------------------------------------------------------------------------- /DeepLearningObjectDetection/Keras-SSD/voc_annotation.py: -------------------------------------------------------------------------------- 1 | import xml.etree.ElementTree as ET 2 | from os import getcwd 3 | 4 | sets=[('2007', 'train'), ('2007', 'val'), ('2007', 'test')] 5 | 6 | classes = ["aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"] 7 | 8 | def convert_annotation(year, image_id, list_file): 9 | in_file = open('VOCdevkit/VOC%s/Annotations/%s.xml'%(year, image_id)) 10 | tree=ET.parse(in_file) 11 | root = tree.getroot() 12 | 13 | for obj in root.iter('object'): 14 | difficult = obj.find('difficult').text 15 | cls = obj.find('name').text 16 | if cls not in classes or int(difficult)==1: 17 | continue 18 | cls_id = classes.index(cls) 19 | xmlbox = obj.find('bndbox') 20 | b = (int(xmlbox.find('xmin').text), int(xmlbox.find('ymin').text), int(xmlbox.find('xmax').text), int(xmlbox.find('ymax').text)) 21 | list_file.write(" " + ",".join([str(a) for a in b]) + ',' + str(cls_id)) 22 | 23 | wd = getcwd() 24 | 25 | for year, image_set in sets: 26 | image_ids = open('VOCdevkit/VOC%s/ImageSets/Main/%s.txt'%(year, image_set)).read().strip().split() 27 | list_file = open('%s_%s.txt'%(year, image_set), 'w') 28 | for image_id in image_ids: 29 | list_file.write('%s/VOCdevkit/VOC%s/JPEGImages/%s.jpg'%(wd, year, image_id)) 30 | convert_annotation(year, image_id, list_file) 31 | list_file.write('\n') 32 | list_file.close() 33 | -------------------------------------------------------------------------------- /DeepLearningObjectDetection/PyTorch-YOLOv3/assets/dog.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/DeepLearningObjectDetection/PyTorch-YOLOv3/assets/dog.png -------------------------------------------------------------------------------- /DeepLearningObjectDetection/PyTorch-YOLOv3/assets/giraffe.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/DeepLearningObjectDetection/PyTorch-YOLOv3/assets/giraffe.png -------------------------------------------------------------------------------- /DeepLearningObjectDetection/PyTorch-YOLOv3/assets/messi.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/DeepLearningObjectDetection/PyTorch-YOLOv3/assets/messi.png -------------------------------------------------------------------------------- /DeepLearningObjectDetection/PyTorch-YOLOv3/assets/traffic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/DeepLearningObjectDetection/PyTorch-YOLOv3/assets/traffic.png -------------------------------------------------------------------------------- /DeepLearningObjectDetection/PyTorch-YOLOv3/config/coco.data: -------------------------------------------------------------------------------- 1 | classes= 80 2 | train=data/coco/trainvalno5k.txt 3 | valid=data/coco/5k.txt 4 | names=data/coco.names 5 | backup=backup/ 6 | eval=coco 7 | -------------------------------------------------------------------------------- /DeepLearningObjectDetection/PyTorch-YOLOv3/config/custom.data: -------------------------------------------------------------------------------- 1 | classes= 1 2 | train=data/custom/train.txt 3 | valid=data/custom/valid.txt 4 | names=data/custom/classes.names 5 | -------------------------------------------------------------------------------- /DeepLearningObjectDetection/PyTorch-YOLOv3/config/yolov3-tiny.cfg: -------------------------------------------------------------------------------- 1 | [net] 2 | # Testing 3 | batch=1 4 | subdivisions=1 5 | # Training 6 | # batch=64 7 | # subdivisions=2 8 | width=416 9 | height=416 10 | channels=3 11 | momentum=0.9 12 | decay=0.0005 13 | angle=0 14 | saturation = 1.5 15 | exposure = 1.5 16 | hue=.1 17 | 18 | learning_rate=0.001 19 | burn_in=1000 20 | max_batches = 500200 21 | policy=steps 22 | steps=400000,450000 23 | scales=.1,.1 24 | 25 | # 0 26 | [convolutional] 27 | batch_normalize=1 28 | filters=16 29 | size=3 30 | stride=1 31 | pad=1 32 | activation=leaky 33 | 34 | # 1 35 | [maxpool] 36 | size=2 37 | stride=2 38 | 39 | # 2 40 | [convolutional] 41 | batch_normalize=1 42 | filters=32 43 | size=3 44 | stride=1 45 | pad=1 46 | activation=leaky 47 | 48 | # 3 49 | [maxpool] 50 | size=2 51 | stride=2 52 | 53 | # 4 54 | [convolutional] 55 | batch_normalize=1 56 | filters=64 57 | size=3 58 | stride=1 59 | pad=1 60 | activation=leaky 61 | 62 | # 5 63 | [maxpool] 64 | size=2 65 | stride=2 66 | 67 | # 6 68 | [convolutional] 69 | batch_normalize=1 70 | filters=128 71 | size=3 72 | stride=1 73 | pad=1 74 | activation=leaky 75 | 76 | # 7 77 | [maxpool] 78 | size=2 79 | stride=2 80 | 81 | # 8 82 | [convolutional] 83 | batch_normalize=1 84 | filters=256 85 | size=3 86 | stride=1 87 | pad=1 88 | activation=leaky 89 | 90 | # 9 91 | [maxpool] 92 | size=2 93 | stride=2 94 | 95 | # 10 96 | [convolutional] 97 | batch_normalize=1 98 | filters=512 99 | size=3 100 | stride=1 101 | pad=1 102 | activation=leaky 103 | 104 | # 11 105 | [maxpool] 106 | size=2 107 | stride=1 108 | 109 | # 12 110 | [convolutional] 111 | batch_normalize=1 112 | filters=1024 113 | size=3 114 | stride=1 115 | pad=1 116 | activation=leaky 117 | 118 | ########### 119 | 120 | # 13 121 | [convolutional] 122 | batch_normalize=1 123 | filters=256 124 | size=1 125 | stride=1 126 | pad=1 127 | activation=leaky 128 | 129 | # 14 130 | [convolutional] 131 | batch_normalize=1 132 | filters=512 133 | size=3 134 | stride=1 135 | pad=1 136 | activation=leaky 137 | 138 | # 15 139 | [convolutional] 140 | size=1 141 | stride=1 142 | pad=1 143 | filters=255 144 | activation=linear 145 | 146 | 147 | 148 | # 16 149 | [yolo] 150 | mask = 3,4,5 151 | anchors = 10,14, 23,27, 37,58, 81,82, 135,169, 344,319 152 | classes=80 153 | num=6 154 | jitter=.3 155 | ignore_thresh = .7 156 | truth_thresh = 1 157 | random=1 158 | 159 | # 17 160 | [route] 161 | layers = -4 162 | 163 | # 18 164 | [convolutional] 165 | batch_normalize=1 166 | filters=128 167 | size=1 168 | stride=1 169 | pad=1 170 | activation=leaky 171 | 172 | # 19 173 | [upsample] 174 | stride=2 175 | 176 | # 20 177 | [route] 178 | layers = -1, 8 179 | 180 | # 21 181 | [convolutional] 182 | batch_normalize=1 183 | filters=256 184 | size=3 185 | stride=1 186 | pad=1 187 | activation=leaky 188 | 189 | # 22 190 | [convolutional] 191 | size=1 192 | stride=1 193 | pad=1 194 | filters=255 195 | activation=linear 196 | 197 | # 23 198 | [yolo] 199 | mask = 1,2,3 200 | anchors = 10,14, 23,27, 37,58, 81,82, 135,169, 344,319 201 | classes=80 202 | num=6 203 | jitter=.3 204 | ignore_thresh = .7 205 | truth_thresh = 1 206 | random=1 207 | -------------------------------------------------------------------------------- /DeepLearningObjectDetection/PyTorch-YOLOv3/data/coco.names: -------------------------------------------------------------------------------- 1 | person 2 | bicycle 3 | car 4 | motorbike 5 | aeroplane 6 | bus 7 | train 8 | truck 9 | boat 10 | traffic light 11 | fire hydrant 12 | stop sign 13 | parking meter 14 | bench 15 | bird 16 | cat 17 | dog 18 | horse 19 | sheep 20 | cow 21 | elephant 22 | bear 23 | zebra 24 | giraffe 25 | backpack 26 | umbrella 27 | handbag 28 | tie 29 | suitcase 30 | frisbee 31 | skis 32 | snowboard 33 | sports ball 34 | kite 35 | baseball bat 36 | baseball glove 37 | skateboard 38 | surfboard 39 | tennis racket 40 | bottle 41 | wine glass 42 | cup 43 | fork 44 | knife 45 | spoon 46 | bowl 47 | banana 48 | apple 49 | sandwich 50 | orange 51 | broccoli 52 | carrot 53 | hot dog 54 | pizza 55 | donut 56 | cake 57 | chair 58 | sofa 59 | pottedplant 60 | bed 61 | diningtable 62 | toilet 63 | tvmonitor 64 | laptop 65 | mouse 66 | remote 67 | keyboard 68 | cell phone 69 | microwave 70 | oven 71 | toaster 72 | sink 73 | refrigerator 74 | book 75 | clock 76 | vase 77 | scissors 78 | teddy bear 79 | hair drier 80 | toothbrush 81 | -------------------------------------------------------------------------------- /DeepLearningObjectDetection/PyTorch-YOLOv3/data/custom/classes.names: -------------------------------------------------------------------------------- 1 | train 2 | -------------------------------------------------------------------------------- /DeepLearningObjectDetection/PyTorch-YOLOv3/data/custom/images/train.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/DeepLearningObjectDetection/PyTorch-YOLOv3/data/custom/images/train.jpg -------------------------------------------------------------------------------- /DeepLearningObjectDetection/PyTorch-YOLOv3/data/custom/labels/train.txt: -------------------------------------------------------------------------------- 1 | 0 0.515 0.5 0.21694873 0.18286777 2 | -------------------------------------------------------------------------------- /DeepLearningObjectDetection/PyTorch-YOLOv3/data/custom/train.txt: -------------------------------------------------------------------------------- 1 | data/custom/images/train.jpg 2 | -------------------------------------------------------------------------------- /DeepLearningObjectDetection/PyTorch-YOLOv3/data/custom/valid.txt: -------------------------------------------------------------------------------- 1 | data/custom/images/train.jpg 2 | -------------------------------------------------------------------------------- /DeepLearningObjectDetection/PyTorch-YOLOv3/data/get_coco_dataset.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # CREDIT: https://github.com/pjreddie/darknet/tree/master/scripts/get_coco_dataset.sh 4 | 5 | # Clone COCO API 6 | git clone https://github.com/pdollar/coco 7 | cd coco 8 | 9 | mkdir images 10 | cd images 11 | 12 | # Download Images 13 | wget -c https://pjreddie.com/media/files/train2014.zip 14 | wget -c https://pjreddie.com/media/files/val2014.zip 15 | 16 | # Unzip 17 | unzip -q train2014.zip 18 | unzip -q val2014.zip 19 | 20 | cd .. 21 | 22 | # Download COCO Metadata 23 | wget -c https://pjreddie.com/media/files/instances_train-val2014.zip 24 | wget -c https://pjreddie.com/media/files/coco/5k.part 25 | wget -c https://pjreddie.com/media/files/coco/trainvalno5k.part 26 | wget -c https://pjreddie.com/media/files/coco/labels.tgz 27 | tar xzf labels.tgz 28 | unzip -q instances_train-val2014.zip 29 | 30 | # Set Up Image Lists 31 | paste <(awk "{print \"$PWD\"}" <5k.part) 5k.part | tr -d '\t' > 5k.txt 32 | paste <(awk "{print \"$PWD\"}" trainvalno5k.txt 33 | -------------------------------------------------------------------------------- /DeepLearningObjectDetection/PyTorch-YOLOv3/data/samples/dog.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/DeepLearningObjectDetection/PyTorch-YOLOv3/data/samples/dog.jpg -------------------------------------------------------------------------------- /DeepLearningObjectDetection/PyTorch-YOLOv3/data/samples/eagle.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/DeepLearningObjectDetection/PyTorch-YOLOv3/data/samples/eagle.jpg -------------------------------------------------------------------------------- /DeepLearningObjectDetection/PyTorch-YOLOv3/data/samples/field.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/DeepLearningObjectDetection/PyTorch-YOLOv3/data/samples/field.jpg -------------------------------------------------------------------------------- /DeepLearningObjectDetection/PyTorch-YOLOv3/data/samples/giraffe.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/DeepLearningObjectDetection/PyTorch-YOLOv3/data/samples/giraffe.jpg -------------------------------------------------------------------------------- /DeepLearningObjectDetection/PyTorch-YOLOv3/data/samples/herd_of_horses.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/DeepLearningObjectDetection/PyTorch-YOLOv3/data/samples/herd_of_horses.jpg -------------------------------------------------------------------------------- /DeepLearningObjectDetection/PyTorch-YOLOv3/data/samples/messi.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/DeepLearningObjectDetection/PyTorch-YOLOv3/data/samples/messi.jpg -------------------------------------------------------------------------------- /DeepLearningObjectDetection/PyTorch-YOLOv3/data/samples/person.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/DeepLearningObjectDetection/PyTorch-YOLOv3/data/samples/person.jpg -------------------------------------------------------------------------------- /DeepLearningObjectDetection/PyTorch-YOLOv3/data/samples/room.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/DeepLearningObjectDetection/PyTorch-YOLOv3/data/samples/room.jpg -------------------------------------------------------------------------------- /DeepLearningObjectDetection/PyTorch-YOLOv3/data/samples/street.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/DeepLearningObjectDetection/PyTorch-YOLOv3/data/samples/street.jpg -------------------------------------------------------------------------------- /DeepLearningObjectDetection/PyTorch-YOLOv3/data/train.txt: -------------------------------------------------------------------------------- 1 | data/custom/images/train.jpg 2 | -------------------------------------------------------------------------------- /DeepLearningObjectDetection/PyTorch-YOLOv3/data/valid.txt: -------------------------------------------------------------------------------- 1 | data/custom/images/train.jpg 2 | -------------------------------------------------------------------------------- /DeepLearningObjectDetection/PyTorch-YOLOv3/requirements.txt: -------------------------------------------------------------------------------- 1 | numpy 2 | torch>=1.0 3 | torchvision 4 | matplotlib 5 | tensorflow 6 | tensorboard 7 | terminaltables 8 | pillow 9 | tqdm 10 | -------------------------------------------------------------------------------- /DeepLearningObjectDetection/PyTorch-YOLOv3/test.py: -------------------------------------------------------------------------------- 1 | from __future__ import division 2 | 3 | from models import * 4 | from utils.utils import * 5 | from utils.datasets import * 6 | from utils.parse_config import * 7 | 8 | import os 9 | import sys 10 | import time 11 | import datetime 12 | import argparse 13 | import tqdm 14 | 15 | import torch 16 | from torch.utils.data import DataLoader 17 | from torchvision import datasets 18 | from torchvision import transforms 19 | from torch.autograd import Variable 20 | import torch.optim as optim 21 | 22 | 23 | def evaluate(model, path, iou_thres, conf_thres, nms_thres, img_size, batch_size): 24 | model.eval() 25 | 26 | # Get dataloader 27 | dataset = ListDataset(path, img_size=img_size, augment=False, multiscale=False) 28 | dataloader = torch.utils.data.DataLoader( 29 | dataset, batch_size=batch_size, shuffle=False, num_workers=1, collate_fn=dataset.collate_fn 30 | ) 31 | 32 | Tensor = torch.cuda.FloatTensor if torch.cuda.is_available() else torch.FloatTensor 33 | 34 | labels = [] 35 | sample_metrics = [] # List of tuples (TP, confs, pred) 36 | for batch_i, (_, imgs, targets) in enumerate(tqdm.tqdm(dataloader, desc="Detecting objects")): 37 | 38 | # Extract labels 39 | labels += targets[:, 1].tolist() 40 | # Rescale target 41 | targets[:, 2:] = xywh2xyxy(targets[:, 2:]) 42 | targets[:, 2:] *= img_size 43 | 44 | imgs = Variable(imgs.type(Tensor), requires_grad=False) 45 | 46 | with torch.no_grad(): 47 | outputs = model(imgs) 48 | outputs = non_max_suppression(outputs, conf_thres=conf_thres, nms_thres=nms_thres) 49 | 50 | sample_metrics += get_batch_statistics(outputs, targets, iou_threshold=iou_thres) 51 | 52 | # Concatenate sample statistics 53 | true_positives, pred_scores, pred_labels = [np.concatenate(x, 0) for x in list(zip(*sample_metrics))] 54 | precision, recall, AP, f1, ap_class = ap_per_class(true_positives, pred_scores, pred_labels, labels) 55 | 56 | return precision, recall, AP, f1, ap_class 57 | 58 | 59 | if __name__ == "__main__": 60 | parser = argparse.ArgumentParser() 61 | parser.add_argument("--batch_size", type=int, default=8, help="size of each image batch") 62 | parser.add_argument("--model_def", type=str, default="config/yolov3.cfg", help="path to model definition file") 63 | parser.add_argument("--data_config", type=str, default="config/coco.data", help="path to data config file") 64 | parser.add_argument("--weights_path", type=str, default="weights/yolov3.weights", help="path to weights file") 65 | parser.add_argument("--class_path", type=str, default="data/coco.names", help="path to class label file") 66 | parser.add_argument("--iou_thres", type=float, default=0.5, help="iou threshold required to qualify as detected") 67 | parser.add_argument("--conf_thres", type=float, default=0.001, help="object confidence threshold") 68 | parser.add_argument("--nms_thres", type=float, default=0.5, help="iou thresshold for non-maximum suppression") 69 | parser.add_argument("--n_cpu", type=int, default=8, help="number of cpu threads to use during batch generation") 70 | parser.add_argument("--img_size", type=int, default=416, help="size of each image dimension") 71 | opt = parser.parse_args() 72 | print(opt) 73 | 74 | device = torch.device("cuda" if torch.cuda.is_available() else "cpu") 75 | 76 | data_config = parse_data_config(opt.data_config) 77 | valid_path = data_config["valid"] 78 | class_names = load_classes(data_config["names"]) 79 | 80 | # Initiate model 81 | model = Darknet(opt.model_def).to(device) 82 | if opt.weights_path.endswith(".weights"): 83 | # Load darknet weights 84 | model.load_darknet_weights(opt.weights_path) 85 | else: 86 | # Load checkpoint weights 87 | model.load_state_dict(torch.load(opt.weights_path)) 88 | 89 | print("Compute mAP...") 90 | 91 | precision, recall, AP, f1, ap_class = evaluate( 92 | model, 93 | path=valid_path, 94 | iou_thres=opt.iou_thres, 95 | conf_thres=opt.conf_thres, 96 | nms_thres=opt.nms_thres, 97 | img_size=opt.img_size, 98 | batch_size=8, 99 | ) 100 | 101 | print("Average Precisions:") 102 | for i, c in enumerate(ap_class): 103 | print(f"+ Class '{c}' ({class_names[c]}) - AP: {AP[i]}") 104 | 105 | print(f"mAP: {AP.mean()}") 106 | -------------------------------------------------------------------------------- /DeepLearningObjectDetection/PyTorch-YOLOv3/utils/augmentations.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn.functional as F 3 | import numpy as np 4 | 5 | 6 | def horisontal_flip(images, targets): 7 | images = torch.flip(images, [-1]) 8 | targets[:, 2] = 1 - targets[:, 2] 9 | return images, targets 10 | -------------------------------------------------------------------------------- /DeepLearningObjectDetection/PyTorch-YOLOv3/utils/datasets.py: -------------------------------------------------------------------------------- 1 | import glob 2 | import random 3 | import os 4 | import sys 5 | import numpy as np 6 | from PIL import Image 7 | import torch 8 | import torch.nn.functional as F 9 | 10 | from utils.augmentations import horisontal_flip 11 | from torch.utils.data import Dataset 12 | import torchvision.transforms as transforms 13 | 14 | 15 | def pad_to_square(img, pad_value): 16 | c, h, w = img.shape 17 | dim_diff = np.abs(h - w) 18 | # (upper / left) padding and (lower / right) padding 19 | pad1, pad2 = dim_diff // 2, dim_diff - dim_diff // 2 20 | # Determine padding 21 | pad = (0, 0, pad1, pad2) if h <= w else (pad1, pad2, 0, 0) 22 | # Add padding 23 | img = F.pad(img, pad, "constant", value=pad_value) 24 | 25 | return img, pad 26 | 27 | 28 | def resize(image, size): 29 | image = F.interpolate(image.unsqueeze(0), size=size, mode="nearest").squeeze(0) 30 | return image 31 | 32 | 33 | def random_resize(images, min_size=288, max_size=448): 34 | new_size = random.sample(list(range(min_size, max_size + 1, 32)), 1)[0] 35 | images = F.interpolate(images, size=new_size, mode="nearest") 36 | return images 37 | 38 | 39 | class ImageFolder(Dataset): 40 | def __init__(self, folder_path, img_size=416): 41 | self.files = sorted(glob.glob("%s/*.*" % folder_path)) 42 | self.img_size = img_size 43 | 44 | def __getitem__(self, index): 45 | img_path = self.files[index % len(self.files)] 46 | # Extract image as PyTorch tensor 47 | img = transforms.ToTensor()(Image.open(img_path)) 48 | # Pad to square resolution 49 | img, _ = pad_to_square(img, 0) 50 | # Resize 51 | img = resize(img, self.img_size) 52 | 53 | return img_path, img 54 | 55 | def __len__(self): 56 | return len(self.files) 57 | 58 | 59 | class ListDataset(Dataset): 60 | def __init__(self, list_path, img_size=416, augment=True, multiscale=True, normalized_labels=True): 61 | with open(list_path, "r") as file: 62 | self.img_files = file.readlines() 63 | 64 | self.label_files = [ 65 | path.replace("images", "labels").replace(".png", ".txt").replace(".jpg", ".txt") 66 | for path in self.img_files 67 | ] 68 | self.img_size = img_size 69 | self.max_objects = 100 70 | self.augment = augment 71 | self.multiscale = multiscale 72 | self.normalized_labels = normalized_labels 73 | self.min_size = self.img_size - 3 * 32 74 | self.max_size = self.img_size + 3 * 32 75 | self.batch_count = 0 76 | 77 | def __getitem__(self, index): 78 | 79 | # --------- 80 | # Image 81 | # --------- 82 | 83 | img_path = self.img_files[index % len(self.img_files)].rstrip() 84 | 85 | # Extract image as PyTorch tensor 86 | img = transforms.ToTensor()(Image.open(img_path).convert('RGB')) 87 | 88 | # Handle images with less than three channels 89 | if len(img.shape) != 3: 90 | img = img.unsqueeze(0) 91 | img = img.expand((3, img.shape[1:])) 92 | 93 | _, h, w = img.shape 94 | h_factor, w_factor = (h, w) if self.normalized_labels else (1, 1) 95 | # Pad to square resolution 96 | img, pad = pad_to_square(img, 0) 97 | _, padded_h, padded_w = img.shape 98 | 99 | # --------- 100 | # Label 101 | # --------- 102 | 103 | label_path = self.label_files[index % len(self.img_files)].rstrip() 104 | 105 | targets = None 106 | if os.path.exists(label_path): 107 | boxes = torch.from_numpy(np.loadtxt(label_path).reshape(-1, 5)) 108 | # Extract coordinates for unpadded + unscaled image 109 | x1 = w_factor * (boxes[:, 1] - boxes[:, 3] / 2) 110 | y1 = h_factor * (boxes[:, 2] - boxes[:, 4] / 2) 111 | x2 = w_factor * (boxes[:, 1] + boxes[:, 3] / 2) 112 | y2 = h_factor * (boxes[:, 2] + boxes[:, 4] / 2) 113 | # Adjust for added padding 114 | x1 += pad[0] 115 | y1 += pad[2] 116 | x2 += pad[1] 117 | y2 += pad[3] 118 | # Returns (x, y, w, h) 119 | boxes[:, 1] = ((x1 + x2) / 2) / padded_w 120 | boxes[:, 2] = ((y1 + y2) / 2) / padded_h 121 | boxes[:, 3] *= w_factor / padded_w 122 | boxes[:, 4] *= h_factor / padded_h 123 | 124 | targets = torch.zeros((len(boxes), 6)) 125 | targets[:, 1:] = boxes 126 | 127 | # Apply augmentations 128 | if self.augment: 129 | if np.random.random() < 0.5: 130 | img, targets = horisontal_flip(img, targets) 131 | 132 | return img_path, img, targets 133 | 134 | def collate_fn(self, batch): 135 | paths, imgs, targets = list(zip(*batch)) 136 | # Remove empty placeholder targets 137 | targets = [boxes for boxes in targets if boxes is not None] 138 | # Add sample index to targets 139 | for i, boxes in enumerate(targets): 140 | boxes[:, 0] = i 141 | targets = torch.cat(targets, 0) 142 | # Selects new image size every tenth batch 143 | if self.multiscale and self.batch_count % 10 == 0: 144 | self.img_size = random.choice(range(self.min_size, self.max_size + 1, 32)) 145 | # Resize images to input shape 146 | imgs = torch.stack([resize(img, self.img_size) for img in imgs]) 147 | self.batch_count += 1 148 | return paths, imgs, targets 149 | 150 | def __len__(self): 151 | return len(self.img_files) 152 | -------------------------------------------------------------------------------- /DeepLearningObjectDetection/PyTorch-YOLOv3/utils/logger.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | 3 | 4 | class Logger(object): 5 | def __init__(self, log_dir): 6 | """Create a summary writer logging to log_dir.""" 7 | self.writer = tf.summary.FileWriter(log_dir) 8 | 9 | def scalar_summary(self, tag, value, step): 10 | """Log a scalar variable.""" 11 | summary = tf.Summary(value=[tf.Summary.Value(tag=tag, simple_value=value)]) 12 | self.writer.add_summary(summary, step) 13 | 14 | def list_of_scalars_summary(self, tag_value_pairs, step): 15 | """Log scalar variables.""" 16 | summary = tf.Summary(value=[tf.Summary.Value(tag=tag, simple_value=value) for tag, value in tag_value_pairs]) 17 | self.writer.add_summary(summary, step) 18 | -------------------------------------------------------------------------------- /DeepLearningObjectDetection/PyTorch-YOLOv3/utils/parse_config.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | def parse_model_config(path): 4 | """Parses the yolo-v3 layer configuration file and returns module definitions""" 5 | file = open(path, 'r') 6 | lines = file.read().split('\n') 7 | lines = [x for x in lines if x and not x.startswith('#')] 8 | lines = [x.rstrip().lstrip() for x in lines] # get rid of fringe whitespaces 9 | module_defs = [] 10 | for line in lines: 11 | if line.startswith('['): # This marks the start of a new block 12 | module_defs.append({}) 13 | module_defs[-1]['type'] = line[1:-1].rstrip() 14 | if module_defs[-1]['type'] == 'convolutional': 15 | module_defs[-1]['batch_normalize'] = 0 16 | else: 17 | key, value = line.split("=") 18 | value = value.strip() 19 | module_defs[-1][key.rstrip()] = value.strip() 20 | 21 | return module_defs 22 | 23 | def parse_data_config(path): 24 | """Parses the data configuration file""" 25 | options = dict() 26 | options['gpus'] = '0,1,2,3' 27 | options['num_workers'] = '10' 28 | with open(path, 'r') as fp: 29 | lines = fp.readlines() 30 | for line in lines: 31 | line = line.strip() 32 | if line == '' or line.startswith('#'): 33 | continue 34 | key, value = line.split('=') 35 | options[key.strip()] = value.strip() 36 | return options 37 | -------------------------------------------------------------------------------- /DeepLearningObjectDetection/README.md: -------------------------------------------------------------------------------- 1 | ## 目标检测学习笔记 2 | - 这里整理了一下 YOLO的代码和ssd的代码。(等有空了,将博客上传,先将fork大神的代码整理到这里) 3 | - 这些都是我摘干净的,可以直接跑的 4 | -------------------------------------------------------------------------------- /DeepLearningPapersTranslation/README.md: -------------------------------------------------------------------------------- 1 | # 深度学习论文翻译地址 2 | *** 3 | ###### 摘要 4 | > 这个文件夹存放的是我深度学习论文翻译的博客地址,希望可以帮助到别人 5 | *** 6 | ### 深度学习论文翻译解析(一) 7 | - YOLOv3: An Incremental Improvement 8 | - 地址:https://www.cnblogs.com/wj-1314/p/9744146.html 9 | *** 10 | ### 深度学习论文翻译解析(二) 11 | - An End-to-End Trainable Neural Network for Image-based Sequence Recognition and Its Application to Scene Text Recognition 12 | - 地址:https://www.cnblogs.com/wj-1314/p/9901029.html 13 | *** 14 | ### 深度学习论文翻译解析(三) 15 | - Detecting Text in Natural Image with Connectionist Text Proposal Network 16 | - 地址:https://www.cnblogs.com/wj-1314/p/9913406.html 17 | *** 18 | ### 深度学习论文翻译解析(四) 19 | - Faster R-CNN: Down the rabbit hole of modern object detection 20 | - 地址:https://www.cnblogs.com/wj-1314/p/9914304.html 21 | *** 22 | ### 深度学习论文翻译解析(五) 23 | - Siamese Neural Networks for One-shot Image Recognition 24 | - 地址:https://www.cnblogs.com/wj-1314/p/11583808.html 25 | *** 26 | ### 深度学习论文翻译解析(六) 27 | - MobileNets:Efficient Convolutional Neural Networks for Mobile Vision Appliications 28 | - 地址:https://www.cnblogs.com/wj-1314/p/12118369.html 29 | *** 30 | ### 深度学习论文翻译解析(七) 31 | - Support Vector Method for Novelty Detection 32 | - 地址:https://www.cnblogs.com/wj-1314/p/12170547.html 33 | *** 34 | ### 深度学习论文翻译解析(八) 35 | - Rich feature hierarchies for accurate object detection and semantic segmentation 36 | - 地址:https://www.cnblogs.com/wj-1314/p/13209009.html 37 | *** 38 | ### 深度学习论文翻译解析(九) 39 | - Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition 40 | - 地址:https://www.cnblogs.com/wj-1314/p/13214500.html 41 | *** 42 | ### 深度学习论文翻译解析(十) 43 | - Visualizing and Understanding Convolutional Networks 44 | - 地址:https://www.cnblogs.com/wj-1314/p/13272191.html 45 | *** 46 | ### 深度学习论文翻译解析(十一) 47 | - OverFeat: Integrated Recognition, Localization and Detection using Convolutional Networks 48 | - 地址:https://www.cnblogs.com/wj-1314/p/13331182.html 49 | *** 50 | ### 深度学习论文翻译解析(十二) 51 | - Fast R-CNN 52 | - 地址:https://www.cnblogs.com/wj-1314/p/13359146.html 53 | *** 54 | ### 深度学习论文翻译解析(十三) 55 | - Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks 56 | - 地址:https://www.cnblogs.com/wj-1314/p/13404831.html 57 | *** 58 | ### 深度学习论文翻译解析(十四) 59 | - SSD: Single Shot MultiBox Detector 60 | - 地址:https://www.cnblogs.com/wj-1314/p/11955967.html 61 | *** 62 | ### 深度学习论文翻译解析(十五) 63 | - Densely Connected Convolutional Networks 64 | - 地址:https://www.cnblogs.com/wj-1314/p/14025211.html 65 | *** 66 | ### 深度学习论文翻译解析(十六) 67 | - Squeeze-and-Excitation Networks 68 | - 地址:https://www.cnblogs.com/wj-1314/p/14143105.html 69 | *** 70 | ### 深度学习论文翻译解析(十七) 71 | - MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications 72 | - 地址:https://www.cnblogs.com/wj-1314/p/14318311.html 73 | *** 74 | ### 深度学习论文翻译解析(十八) 75 | - MobileNetV2: Inverted Residuals and Linear Bottlenecks 76 | - 地址:https://www.cnblogs.com/wj-1314/p/14077776.html 77 | *** 78 | ### 深度学习论文翻译解析(十九) 79 | - Searching for MobileNetV3 80 | - 地址:https://www.cnblogs.com/wj-1314/p/12108424.html 81 | *** 82 | ### 深度学习论文翻译解析(二十) 83 | - YOLOv4: Optimal Speed and Accuracy of Object Detection 84 | - 地址:https://www.cnblogs.com/wj-1314/p/14506507.html 85 | *** 86 | -------------------------------------------------------------------------------- /KerasLearningNote/Keras-my-train-model/README.md: -------------------------------------------------------------------------------- 1 | 比如 lenet_1_1 lenet_1_3 前后数字的意思不同:前面数字(1代表数据不增强,2代表数据增强),后面数字(1代表一通道,灰度图,3代表三通道,RGB图) 2 | -------------------------------------------------------------------------------- /KerasLearningNote/Keras-my-train-model/custom_layers.py: -------------------------------------------------------------------------------- 1 | from keras.engine import Layer, InputSpec 2 | try: 3 | from keras import initializations 4 | except ImportError: 5 | from keras import initializers as initializations 6 | import keras.backend as K 7 | 8 | class Scale(Layer): 9 | '''Custom Layer for DenseNet used for BatchNormalization. 10 | 11 | Learns a set of weights and biases used for scaling the input data. 12 | the output consists simply in an element-wise multiplication of the input 13 | and a sum of a set of constants: 14 | out = in * gamma + beta, 15 | where 'gamma' and 'beta' are the weights and biases larned. 16 | # Arguments 17 | axis: integer, axis along which to normalize in mode 0. For instance, 18 | if your input tensor has shape (samples, channels, rows, cols), 19 | set axis to 1 to normalize per feature map (channels axis). 20 | momentum: momentum in the computation of the 21 | exponential average of the mean and standard deviation 22 | of the data, for feature-wise normalization. 23 | weights: Initialization weights. 24 | List of 2 Numpy arrays, with shapes: 25 | `[(input_shape,), (input_shape,)]` 26 | beta_init: name of initialization function for shift parameter 27 | (see [initializations](../initializations.md)), or alternatively, 28 | Theano/TensorFlow function to use for weights initialization. 29 | This parameter is only relevant if you don't pass a `weights` argument. 30 | gamma_init: name of initialization function for scale parameter (see 31 | [initializations](../initializations.md)), or alternatively, 32 | Theano/TensorFlow function to use for weights initialization. 33 | This parameter is only relevant if you don't pass a `weights` argument. 34 | ''' 35 | def __init__(self, weights=None, axis=-1, momentum = 0.9, beta_init='zero', gamma_init='one', **kwargs): 36 | self.momentum = momentum 37 | self.axis = axis 38 | self.beta_init = initializations.get(beta_init) 39 | self.gamma_init = initializations.get(gamma_init) 40 | self.initial_weights = weights 41 | super(Scale, self).__init__(**kwargs) 42 | 43 | def build(self, input_shape): 44 | self.input_spec = [InputSpec(shape=input_shape)] 45 | shape = (int(input_shape[self.axis]),) 46 | 47 | # Tensorflow >= 1.0.0 compatibility 48 | self.gamma = K.variable(self.gamma_init(shape), name='{}_gamma'.format(self.name)) 49 | self.beta = K.variable(self.beta_init(shape), name='{}_beta'.format(self.name)) 50 | #self.gamma = self.gamma_init(shape, name='{}_gamma'.format(self.name)) 51 | #self.beta = self.beta_init(shape, name='{}_beta'.format(self.name)) 52 | self.trainable_weights = [self.gamma, self.beta] 53 | 54 | if self.initial_weights is not None: 55 | self.set_weights(self.initial_weights) 56 | del self.initial_weights 57 | 58 | def call(self, x, mask=None): 59 | input_shape = self.input_spec[0].shape 60 | broadcast_shape = [1] * len(input_shape) 61 | broadcast_shape[self.axis] = input_shape[self.axis] 62 | 63 | out = K.reshape(self.gamma, broadcast_shape) * x + K.reshape(self.beta, broadcast_shape) 64 | return out 65 | 66 | def get_config(self): 67 | config = {"momentum": self.momentum, "axis": self.axis} 68 | base_config = super(Scale, self).get_config() 69 | return dict(list(base_config.items()) + list(config.items())) -------------------------------------------------------------------------------- /KerasLearningNote/Keras-my-train-model/densenetloss121_1_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/Keras-my-train-model/densenetloss121_1_1.jpg -------------------------------------------------------------------------------- /KerasLearningNote/Keras-my-train-model/densenetloss121_1_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/Keras-my-train-model/densenetloss121_1_3.jpg -------------------------------------------------------------------------------- /KerasLearningNote/Keras-my-train-model/densenetloss121_2_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/Keras-my-train-model/densenetloss121_2_1.jpg -------------------------------------------------------------------------------- /KerasLearningNote/Keras-my-train-model/densenetloss121_2_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/Keras-my-train-model/densenetloss121_2_3.jpg -------------------------------------------------------------------------------- /KerasLearningNote/Keras-my-train-model/densenetloss_1_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/Keras-my-train-model/densenetloss_1_1.jpg -------------------------------------------------------------------------------- /KerasLearningNote/Keras-my-train-model/densenetloss_1_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/Keras-my-train-model/densenetloss_1_3.jpg -------------------------------------------------------------------------------- /KerasLearningNote/Keras-my-train-model/densenetloss_2_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/Keras-my-train-model/densenetloss_2_1.jpg -------------------------------------------------------------------------------- /KerasLearningNote/Keras-my-train-model/densenetloss_2_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/Keras-my-train-model/densenetloss_2_3.jpg -------------------------------------------------------------------------------- /KerasLearningNote/Keras-my-train-model/lenetloss_1_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/Keras-my-train-model/lenetloss_1_1.jpg -------------------------------------------------------------------------------- /KerasLearningNote/Keras-my-train-model/lenetloss_1_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/Keras-my-train-model/lenetloss_1_3.jpg -------------------------------------------------------------------------------- /KerasLearningNote/Keras-my-train-model/lenetloss_2_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/Keras-my-train-model/lenetloss_2_1.jpg -------------------------------------------------------------------------------- /KerasLearningNote/Keras-my-train-model/lenetloss_2_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/Keras-my-train-model/lenetloss_2_3.jpg -------------------------------------------------------------------------------- /KerasLearningNote/Keras-my-train-model/myalexnet.py: -------------------------------------------------------------------------------- 1 | ''' 2 | this script is AlexNet model for Keras 3 | 4 | ''' 5 | from keras.models import Model, Sequential 6 | from keras.layers import Flatten, Dense, Input, Conv2D, MaxPooling2D, BatchNormalization, Dropout 7 | from keras.optimizers import Adam 8 | 9 | 10 | def AlexNet(input_shape, classes): 11 | ''' 12 | 13 | :param input_shape: default (227, 227, 3) 14 | :param classes: default 1000 15 | :return: 16 | ''' 17 | model = Sequential() 18 | model.add(Conv2D(96, (11, 11), strides=(4, 4), input_shape=input_shape, 19 | padding='valid', activation='relu', kernel_initializer='uniform', 20 | name='convolution_C1')) 21 | model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='Overlapping_M2')) 22 | model.add(Conv2D(256, (5, 5), strides=(1, 1), padding='same', 23 | activation='relu', kernel_initializer='uniform', 24 | name='convolution_C3')) 25 | model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='Overlapping_M4')) 26 | model.add(Conv2D(384, (3, 3), strides=(1, 1), padding='same', 27 | activation='relu', kernel_initializer='uniform', 28 | name='convolution_C5')) 29 | model.add(Conv2D(384, (3, 3), strides=(1, 1), padding='same', 30 | activation='relu', kernel_initializer='uniform', 31 | name='convolution_C6')) 32 | model.add(Conv2D(256, (3, 3), strides=(1, 1), padding='same', 33 | activation='relu', kernel_initializer='uniform', 34 | name='convolution_C7')) 35 | model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='Overlapping_M8')) 36 | model.add(Flatten(name='Fullconnection_C9')) 37 | model.add(Dense(4096, activation='relu', name='Fullconnection_F10')) 38 | model.add(Dropout(0.5)) 39 | model.add(Dense(4096, activation='relu', name='Fullconnection_F11')) 40 | model.add(Dropout(0.5)) 41 | model.add(Dense(classes, activation='softmax', name='predictions')) 42 | 43 | # model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy']) 44 | model.summary() 45 | return model 46 | 47 | 48 | def AlexNet_model(input_shape, classes): 49 | ''' 50 | 51 | :param input_shape: default (227, 227, 3) 52 | :param classes: default 1000 53 | :return: 54 | ''' 55 | inputs = Input(shape=input_shape) 56 | x = Conv2D(96, (11, 11), strides=(4, 4), padding='valid', activation='relu', 57 | kernel_initializer='uniform', name='convolution_C1')(inputs) 58 | x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='Overlapping_M2')(x) 59 | 60 | x = Conv2D(256, (5, 5), strides=(1, 1), padding='same', 61 | activation='relu', kernel_initializer='uniform', 62 | name='convolution_C3')(x) 63 | x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='Overlapping_M4')(x) 64 | 65 | x = Conv2D(384, (3, 3), strides=(1, 1), padding='same', 66 | activation='relu', kernel_initializer='uniform', 67 | name='convolution_C5')(x) 68 | x = Conv2D(384, (3, 3), strides=(1, 1), padding='same', 69 | activation='relu', kernel_initializer='uniform', 70 | name='convolution_C6')(x) 71 | x = Conv2D(256, (3, 3), strides=(1, 1), padding='same', 72 | activation='relu', kernel_initializer='uniform', 73 | name='convolution_C7')(x) 74 | x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='Overlapping_M8')(x) 75 | 76 | x = Flatten(name='Fullconnection_C9')(x) 77 | x = Dense(4096, activation='relu', name='Fullconnection_F10')(x) 78 | x = Dropout(0.5)(x) 79 | x = Dense(4096, activation='relu', name='Fullconnection_F11')(x) 80 | x = Dropout(0.5)(x) 81 | x = Dense(classes, activation='softmax', name='predictions')(x) 82 | model = Model(inputs, output=x) 83 | 84 | # my_optimizer = Adam(0.001) 85 | # model.compile(optimizer='my_optimizer', loss='categorical_crossentropy', metrics=['accuracy']) 86 | model.summary() 87 | return model 88 | 89 | 90 | if __name__ == '__main__': 91 | # AlexNet(input_shape=(227, 227, 3), classes=1000) 92 | AlexNet_model(input_shape=(227, 227, 3), classes=1000) 93 | -------------------------------------------------------------------------------- /KerasLearningNote/Keras-my-train-model/mylenet.py: -------------------------------------------------------------------------------- 1 | ''' 2 | this script is LeNet model for Keras 3 | 4 | ''' 5 | from keras.models import Model, Sequential 6 | from keras.layers import Flatten, Dense, Input, Conv2D, MaxPooling2D 7 | 8 | 9 | def LeNet(input_shape, classes): 10 | ''' 11 | 12 | :param input_shape: default (28, 28, 1) 13 | :param classes: default 10 (because the mnist class label is 10) 14 | :return: 15 | ''' 16 | model = Sequential() 17 | model.add(Conv2D(32, (5, 5), strides=(1, 1), input_shape=input_shape, 18 | padding='valid', activation='relu', kernel_initializer='uniform', 19 | name='convolution_C1')) 20 | model.add(MaxPooling2D(pool_size=(2, 2), name='Subsampling_S2')) 21 | model.add(Conv2D(64, (5, 5), strides=(1, 1), padding='valid', 22 | activation='relu', kernel_initializer='uniform', 23 | name='convolution_C3')) 24 | model.add(MaxPooling2D(pool_size=(2, 2), name='Subsampling_S4')) 25 | model.add(Flatten(name='Fullconnection_C5')) 26 | model.add(Dense(100, activation='relu', name='Fullconnection_F6')) 27 | model.add(Dense(classes, activation='softmax', name='predictions')) 28 | 29 | # model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy']) 30 | model.summary() 31 | return model 32 | 33 | 34 | def LeNet_model(input_shape, classes): 35 | ''' 36 | 37 | :param input_shape: default (28, 28, 1) 38 | :param classes: default 10 (because the mnist class label is 10) 39 | :return: 40 | ''' 41 | inputs = Input(shape=input_shape) 42 | x = Conv2D(32, (5, 5), strides=(1, 1), input_shape=input_shape, 43 | padding='valid', activation='relu', kernel_initializer='uniform', 44 | name='convolution_C1')(inputs) 45 | x = MaxPooling2D(pool_size=(2, 2), name='Subsampling_S2')(x) 46 | x = Conv2D(64, (5, 5), strides=(1, 1), padding='valid', 47 | activation='relu', kernel_initializer='uniform', 48 | name='convolution_C3')(x) 49 | x = MaxPooling2D(pool_size=(2, 2), name='Subsampling_S4')(x) 50 | x = Flatten(name='Fullconnection_C5')(x) 51 | x = Dense(100, activation='relu', name='Fullconnection_F6')(x) 52 | x = Dense(classes, activation='softmax', name='predictions')(x) 53 | model = Model(inputs, output=x) 54 | 55 | # model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy']) 56 | model.summary() 57 | return model 58 | 59 | 60 | if __name__ == '__main__': 61 | LeNet(input_shape=(28, 28, 1), classes=10) 62 | # LeNet_model(input_shape=(28, 28, 1), classes=10) 63 | -------------------------------------------------------------------------------- /KerasLearningNote/Keras-my-train-model/myvggnet.py: -------------------------------------------------------------------------------- 1 | ''' 2 | this script is VGG16 model for Keras 3 | 4 | ''' 5 | import numpy as np 6 | from keras.models import Model 7 | from keras.layers import Flatten, Dense, Input, Conv2D, MaxPooling2D, Reshape 8 | from keras import backend as K 9 | 10 | 11 | def Vgg16(input_shape, classes): 12 | ''' 13 | 14 | :param input_shape: default (224, 224, 3) 15 | :return: 16 | ''' 17 | inputs = Input(shape=input_shape) 18 | 19 | # Block 1 20 | x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1')(inputs) 21 | x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2')(x) 22 | x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x) 23 | 24 | # Block 2 25 | x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1')(x) 26 | x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2')(x) 27 | x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x) 28 | 29 | # Block 3 30 | x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1')(x) 31 | x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2')(x) 32 | x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3')(x) 33 | x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x) 34 | 35 | # Block 4 36 | x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1')(x) 37 | x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2')(x) 38 | x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3')(x) 39 | x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x) 40 | 41 | # Block 5 42 | x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1')(x) 43 | x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2')(x) 44 | x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3')(x) 45 | x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x) 46 | 47 | x = Flatten(name='flatten')(x) 48 | x = Dense(4096, activation='relu', name='fc1')(x) 49 | x = Dense(4096, activation='relu', name='fc2')(x) 50 | x = Dense(classes, activation='softmax', name='predictions')(x) 51 | 52 | # output = Reshape((classes,))(x) 53 | 54 | model = Model(inputs, output=x) 55 | model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy']) 56 | return model 57 | 58 | 59 | def Vgg19(input_shape, classes): 60 | ''' 61 | 62 | :param input_shape: default (224, 224, 3) 63 | :return: 64 | ''' 65 | inputs = Input(shape=input_shape) 66 | 67 | # Block 1 68 | x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1')(inputs) 69 | x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2')(x) 70 | x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x) 71 | 72 | # Block 2 73 | x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1')(x) 74 | x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2')(x) 75 | x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x) 76 | 77 | # Block 3 78 | x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1')(x) 79 | x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2')(x) 80 | x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3')(x) 81 | x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv4')(x) 82 | x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x) 83 | 84 | # Block 4 85 | x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1')(x) 86 | x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2')(x) 87 | x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3')(x) 88 | x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv4')(x) 89 | x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x) 90 | 91 | # Block 5 92 | x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1')(x) 93 | x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2')(x) 94 | x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3')(x) 95 | x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv4')(x) 96 | x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x) 97 | 98 | x = Flatten(name='flatten')(x) 99 | x = Dense(4096, activation='relu', name='fc1')(x) 100 | x = Dense(4096, activation='relu', name='fc2')(x) 101 | x = Dense(classes, activation='softmax', name='predictions')(x) 102 | 103 | # output = Reshape((classes,))(x) 104 | 105 | model = Model(inputs, output=x) 106 | model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy']) 107 | return model 108 | -------------------------------------------------------------------------------- /KerasLearningNote/Keras-my-train-model/myzfnet.py: -------------------------------------------------------------------------------- 1 | ''' 2 | this script is ZFNet model for Keras 3 | 4 | ''' 5 | from keras.models import Model, Sequential 6 | from keras.layers import Flatten, Dense, Input, Conv2D, MaxPooling2D, BatchNormalization, Dropout 7 | from keras.optimizers import Adam 8 | 9 | 10 | def ZFNet(input_shape, classes): 11 | ''' 12 | 13 | :param input_shape: default (227, 227, 3) 14 | :param classes: default 1000 15 | :return: 16 | ''' 17 | model = Sequential() 18 | model.add(Conv2D(96, (7, 7), strides=(2, 2), input_shape=input_shape, 19 | padding='valid', activation='relu', kernel_initializer='uniform', 20 | name='convolution_C1')) 21 | model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='Overlapping_M2')) 22 | model.add(Conv2D(256, (5, 5), strides=(2, 2), padding='same', 23 | activation='relu', kernel_initializer='uniform', 24 | name='convolution_C3')) 25 | model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='Overlapping_M4')) 26 | model.add(Conv2D(384, (3, 3), strides=(1, 1), padding='same', 27 | activation='relu', kernel_initializer='uniform', 28 | name='convolution_C5')) 29 | model.add(Conv2D(384, (3, 3), strides=(1, 1), padding='same', 30 | activation='relu', kernel_initializer='uniform', 31 | name='convolution_C6')) 32 | model.add(Conv2D(256, (3, 3), strides=(1, 1), padding='same', 33 | activation='relu', kernel_initializer='uniform', 34 | name='convolution_C7')) 35 | model.add(MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='Overlapping_M8')) 36 | model.add(Flatten(name='Fullconnection_C9')) 37 | model.add(Dense(4096, activation='relu', name='Fullconnection_F10')) 38 | model.add(Dropout(0.5)) 39 | model.add(Dense(4096, activation='relu', name='Fullconnection_F11')) 40 | model.add(Dropout(0.5)) 41 | model.add(Dense(classes, activation='softmax', name='predictions')) 42 | 43 | # model.compile(optimizer='sgd', loss='categorical_crossentropy', metrics=['accuracy']) 44 | model.summary() 45 | return model 46 | 47 | 48 | def ZFNet_model(input_shape, classes): 49 | ''' 50 | 51 | :param input_shape: default (224, 224, 3) 52 | :param classes: default 1000 53 | :return: 54 | ''' 55 | inputs = Input(shape=input_shape) 56 | x = Conv2D(96, (7, 7), strides=(2, 2), padding='valid', activation='relu', 57 | kernel_initializer='uniform', name='convolution_C1')(inputs) 58 | x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='Overlapping_M2')(x) 59 | 60 | x = Conv2D(256, (5, 5), strides=(2, 2), padding='same', 61 | activation='relu', kernel_initializer='uniform', 62 | name='convolution_C3')(x) 63 | x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='Overlapping_M4')(x) 64 | 65 | x = Conv2D(384, (3, 3), strides=(1, 1), padding='same', 66 | activation='relu', kernel_initializer='uniform', 67 | name='convolution_C5')(x) 68 | x = Conv2D(384, (3, 3), strides=(1, 1), padding='same', 69 | activation='relu', kernel_initializer='uniform', 70 | name='convolution_C6')(x) 71 | x = Conv2D(256, (3, 3), strides=(1, 1), padding='same', 72 | activation='relu', kernel_initializer='uniform', 73 | name='convolution_C7')(x) 74 | x = MaxPooling2D(pool_size=(3, 3), strides=(2, 2), name='Overlapping_M8')(x) 75 | 76 | x = Flatten(name='Fullconnection_C9')(x) 77 | x = Dense(4096, activation='relu', name='Fullconnection_F10')(x) 78 | x = Dropout(0.5)(x) 79 | x = Dense(4096, activation='relu', name='Fullconnection_F11')(x) 80 | x = Dropout(0.5)(x) 81 | x = Dense(classes, activation='softmax', name='predictions')(x) 82 | model = Model(inputs, output=x) 83 | 84 | # my_optimizer = Adam(0.001) 85 | # model.compile(optimizer='my_optimizer', loss='categorical_crossentropy', metrics=['accuracy']) 86 | model.summary() 87 | return model 88 | 89 | 90 | if __name__ == '__main__': 91 | # ZFNet(input_shape=(227, 227, 3), classes=1000) 92 | ZFNet_model(input_shape=(224, 224, 3), classes=1000) 93 | -------------------------------------------------------------------------------- /KerasLearningNote/Keras-my-train-model/resnetloss_1_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/Keras-my-train-model/resnetloss_1_1.jpg -------------------------------------------------------------------------------- /KerasLearningNote/Keras-my-train-model/resnetloss_1_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/Keras-my-train-model/resnetloss_1_3.jpg -------------------------------------------------------------------------------- /KerasLearningNote/Keras-my-train-model/resnetloss_2_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/Keras-my-train-model/resnetloss_2_1.jpg -------------------------------------------------------------------------------- /KerasLearningNote/Keras-my-train-model/resnetloss_2_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/Keras-my-train-model/resnetloss_2_3.jpg -------------------------------------------------------------------------------- /KerasLearningNote/Keras-my-train-model/vggnetloss_1_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/Keras-my-train-model/vggnetloss_1_1.jpg -------------------------------------------------------------------------------- /KerasLearningNote/Keras-my-train-model/vggnetloss_1_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/Keras-my-train-model/vggnetloss_1_3.jpg -------------------------------------------------------------------------------- /KerasLearningNote/Keras-my-train-model/vggnetloss_2_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/Keras-my-train-model/vggnetloss_2_1.jpg -------------------------------------------------------------------------------- /KerasLearningNote/Keras-my-train-model/vggnetloss_2_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/Keras-my-train-model/vggnetloss_2_3.jpg -------------------------------------------------------------------------------- /KerasLearningNote/Keras-my-train-model/zfnetloss_1_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/Keras-my-train-model/zfnetloss_1_1.jpg -------------------------------------------------------------------------------- /KerasLearningNote/Keras-my-train-model/zfnetloss_1_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/Keras-my-train-model/zfnetloss_1_3.jpg -------------------------------------------------------------------------------- /KerasLearningNote/Keras-my-train-model/zfnetloss_2_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/Keras-my-train-model/zfnetloss_2_1.jpg -------------------------------------------------------------------------------- /KerasLearningNote/Keras-my-train-model/zfnetloss_2_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/Keras-my-train-model/zfnetloss_2_3.jpg -------------------------------------------------------------------------------- /KerasLearningNote/KerasCode_1/Keras_h5topb.py: -------------------------------------------------------------------------------- 1 | from keras.models import load_model 2 | from tensorflow.python.framework import graph_util 3 | from keras import backend as K 4 | import tensorflow as tf 5 | import os 6 | 7 | def contrastive_loss(y_true, y_pred): 8 | '''Contrastive loss from Hadsell-et-al.'06 9 | http://yann.lecun.com/exdb/publis/pdf/hadsell-chopra-lecun-06.pdf 10 | ''' 11 | margin = 1 12 | square_pred = K.square(y_pred) 13 | margin_square = K.square(K.maximum(margin - y_pred, 0)) 14 | return K.mean(y_true * square_pred + (1 - y_true) * margin_square) 15 | 16 | h5_file = 'sample_model.h5' 17 | 18 | h5_model = model.save(h5_file, custom_objects={'contrastive_loss': contrastive_loss}) 19 | 20 | 21 | -------------------------------------------------------------------------------- /KerasLearningNote/KerasCode_1/Keras_h5totflile.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | # print(tensorflow.__version__) # 1.14.0 3 | from keras.models import load_model 4 | from tensorflow.python.framework import graph_util 5 | from tensorflow import lite 6 | from keras import backend as K 7 | import os 8 | 9 | 10 | def h5_to_pb(h5_file, output_dir, model_name, out_prefix="output_"): 11 | h5_model = load_model(h5_file, custom_objects={'contrastive_loss': contrastive_loss}) 12 | print(h5_model.input) 13 | # [, ] 14 | print(h5_model.output) # [] 15 | print(len(h5_model.outputs)) # 1 16 | out_nodes = [] 17 | for i in range(len(h5_model.outputs)): 18 | out_nodes.append(out_prefix + str(i + 1)) 19 | # print(out_nodes) # ['output_1'] 20 | tf.identity(h5_model.output[i], out_prefix + str(i + 1)) 21 | sess = K.get_session() 22 | init_graph = sess.graph.as_graph_def() 23 | main_graph = graph_util.convert_variables_to_constants(sess, init_graph, out_nodes) 24 | with tf.gfile.GFile(os.path.join(output_dir, model_name), "wb") as filemodel: 25 | filemodel.write(main_graph.SerializeToString()) 26 | print("pb model: ", {os.path.join(output_dir, model_name)}) 27 | 28 | 29 | def pb_to_tflite(pb_file, tflite_file): 30 | inputs = ["input_1"] # 模型文件的输入节点名称 31 | classes = ["output_1"] # 模型文件的输出节点名称 32 | converter = tf.lite.TocoConverter.from_frozen_graph(pb_file, inputs, classes) 33 | tflite_model = converter.convert() 34 | with open(tflite_file, "wb") as f: 35 | f.write(tflite_model) 36 | 37 | 38 | def contrastive_loss(y_true, y_pred): 39 | '''Contrastive loss from Hadsell-et-al.'06 40 | http://yann.lecun.com/exdb/publis/pdf/hadsell-chopra-lecun-06.pdf 41 | ''' 42 | margin = 1 43 | square_pred = K.square(y_pred) 44 | margin_square = K.square(K.maximum(margin - y_pred, 0)) 45 | return K.mean(y_true * square_pred + (1 - y_true) * margin_square) 46 | 47 | 48 | def h5_to_tflite(h5_file, tflite_file): 49 | converter = lite.TFLiteConverter.from_keras_model_file(h5_file, 50 | custom_objects={'contrastive_loss': contrastive_loss}) 51 | tflite_model = converter.convert() 52 | with open(tflite_file, 'wb') as f: 53 | f.write(tflite_model) 54 | 55 | 56 | if __name__ == '__main__': 57 | h5_file = 'screw_10.h5' 58 | tflite_file = 'screw_10.tflite' 59 | pb_file = 'screw_10.pb' 60 | # h5_to_tflite(h5_file, tflite_file) 61 | h5_to_pb(h5_file=h5_file, model_name=pb_file, output_dir='', ) 62 | # pb_to_tflite(pb_file, tflite_file) 63 | -------------------------------------------------------------------------------- /KerasLearningNote/KerasCode_1/UsingPbPredict.py: -------------------------------------------------------------------------------- 1 | import tensorflow as tf 2 | from tensorflow.python.platform import gfile 3 | import cv2 4 | 5 | 6 | def predict_pb(pb_model_path, image_path1, image_path2, target_size): 7 | ''' 8 | 此处为孪生网络的预测代码 9 | ''' 10 | sess = tf.Session() 11 | with gfile.FastGFile(pb_model_path, 'rb') as f: 12 | graph_def = tf.compat.v1.GraphDef() 13 | graph_def.ParseFromString(f.read()) 14 | sess.graph.as_default() 15 | tf.import_graph_def(graph_def, name='') 16 | # 输入 这里有两个输入 17 | input_x = sess.graph.get_tensor_by_name('input_2:0') 18 | input_y = sess.graph.get_tensor_by_name('input_3:0') 19 | # 输出 20 | op = sess.graph.get_tensor_by_name('lambda_1/Sqrt:0') 21 | 22 | image1 = cv2.imread(image_path1) 23 | image2 = cv2.imread(image_path2) 24 | # 灰度化,并调整尺寸 25 | image1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY) 26 | image1 = cv2.resize(image1, target_size) 27 | image2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY) 28 | image2 = cv2.resize(image2, target_size) 29 | data1 = np.array([image1], dtype='float') / 255.0 30 | data2 = np.array([image2], dtype='float') / 255.0 31 | y_pred = sess.run(op, {input_x: data1, input_y: data2}) 32 | print(y_pred) 33 | -------------------------------------------------------------------------------- /KerasLearningNote/KerasCode_1/keras_parctice1.py: -------------------------------------------------------------------------------- 1 | from keras.models import Sequential 2 | from keras.layers import Dense 3 | 4 | 5 | model = Sequential() 6 | model.add(Dense(units=64, activation='relu', input_dim=100)) 7 | model.add(Dense(units=10, activation='softmax')) 8 | model.compile(loss='categorical_crossentropy', 9 | optimizer='sgd', 10 | metrics=['accuracy']) 11 | # x_train 和 y_train 是 Numpy 数组 -- 就像在 Scikit-Learn API 中一样。 12 | model.fit(x_train, y_train, epochs=5, batch_size=32) 13 | 14 | loss_and_metrics = model.evaluate(x_test, y_test, batch_size=128) 15 | classes = model.predict(x_test, batch_size=128) 16 | 17 | -------------------------------------------------------------------------------- /KerasLearningNote/KerasCode_2/Keras_cifar10.py: -------------------------------------------------------------------------------- 1 | # 要训练模型,首先得知道数据长啥样 2 | from __future__ import print_function 3 | import keras 4 | from keras.datasets import cifar10 5 | from keras.preprocessing.image import ImageDataGenerator 6 | from keras.models import Sequential 7 | from keras.layers import Dense, Dropout, Activation, Flatten 8 | from keras.layers import Conv2D, MaxPooling2D 9 | 10 | batch_size = 32 11 | num_classes = 10 12 | epochs = 100 13 | data_augmentation = True 14 | 15 | # 数据载入 16 | (x_train, y_train), (x_test, y_test) = cifar10.load_data() 17 | 18 | # 多分类标签生成,我们将其由单个标签,生成一个热编码的形式 19 | y_train = keras.utils.to_categorical(y_train, num_classes) 20 | y_test = keras.utils.to_categorical(y_test, num_classes) 21 | 22 | # 网络结构配置 23 | model = Sequential() 24 | model.add(Conv2D(32, (3, 3), padding='same', 25 | input_shape=x_train.shape[1:])) # (32, 32, 3) 26 | model.add(Activation('relu')) 27 | model.add(Conv2D(32, (3, 3))) 28 | model.add(Activation('relu')) 29 | model.add(MaxPooling2D(pool_size=(2, 2))) 30 | model.add(Dropout(0.25)) 31 | 32 | model.add(Conv2D(64, (3, 3), padding='same')) 33 | model.add(Activation('relu')) 34 | model.add(Conv2D(64, (3, 3))) 35 | model.add(Activation('relu')) 36 | model.add(MaxPooling2D(pool_size=(2, 2))) 37 | model.add(Dropout(0.25)) 38 | 39 | model.add(Flatten()) 40 | model.add(Dense(512)) 41 | model.add(Activation('relu')) 42 | model.add(Dropout(num_classes)) 43 | model.add(Activation('softmax')) 44 | 45 | # 训练参数设置 46 | # initiate RMSprop optimizer 47 | opt = keras.optimizers.rmsprop(lr=0.0001, decay=1e-6) 48 | 49 | # Let's train the model using RMSprop 50 | model.compile(loss='categorical_crossentropy', 51 | optimizer=opt, 52 | metrics=['accuracy']) 53 | 54 | # 生成训练数据 55 | x_train = x_train.astype('float32') 56 | x_test = x_test.astype('float32') 57 | x_train /= 255 58 | x_test /= 255 59 | 60 | if not data_augmentation: 61 | print("Not using data augmentation") 62 | model.fit(x_train, y_train, 63 | batch_size=batch_size, 64 | epochs=epochs, 65 | validation_data=(x_test, y_test), 66 | shuffle=True) 67 | else: 68 | print("Using real-time data augmentation") 69 | # this will do preprocessing and realtime data augmentation 70 | datagen = ImageDataGenerator( 71 | featurewise_center=False, # set input mean to 0 over the dataset 72 | samplewise_center=False, # set each sample mean to 0 73 | featurewise_std_normalization=False, # divide inputs by std of the dataset 74 | samplewise_std_normalization=False, # divide each input by its std 75 | zca_whitening=False, # apply ZCA whitening 76 | rotation_range=0, # randomly rotate images in the range (degrees, 0 to 180) 77 | width_shift_range=0.1, # randomly shift images horizontally (fraction of total width) 78 | height_shift_range=0.1, # randomly shift images vertically (fraction of total height) 79 | horizontal_flip=True, # randomly flip images 80 | vertical_flip=False) # randomly flip images 81 | 82 | # Compute quantities required for feature-wise normalization 83 | # (std, mean, and principal components if ZCA whitening is applied). 84 | datagen.fit(x_train) 85 | 86 | # fit训练 87 | # fit the model on batches generated by datagen.flow() 88 | model.fit_generator(datagen.flow(x_train, y_train, 89 | batch_size=batch_size), 90 | epochs=epochs, 91 | validation_data=(x_test, y_test)) 92 | -------------------------------------------------------------------------------- /KerasLearningNote/KerasCode_2/keras_CNNparctice1.py: -------------------------------------------------------------------------------- 1 | #_*_coding:utf-8_*_ 2 | from keras.models import Sequential 3 | from keras.layers import Convolution2D, MaxPooling2D 4 | from keras.layers import Activation, Dropout, Flatten, Dense 5 | from keras.preprocessing.image import ImageDataGenerator 6 | from keras import backend as K 7 | K.set_image_dim_ordering('th') 8 | 9 | # 简单的三层卷积加上ReLU激活函数,再接一个max-pooling层 10 | model = Sequential() 11 | model.add(Convolution2D(32, 3, 3, input_shape=(3, 150, 150))) 12 | model.add(Activation('relu')) 13 | model.add(MaxPooling2D(pool_size=(2, 2))) 14 | 15 | model.add(Convolution2D(32, 3, 3)) 16 | model.add(Activation('relu')) 17 | model.add(MaxPooling2D(pool_size=(2, 2))) 18 | 19 | model.add(Convolution2D(64, 3, 3)) 20 | model.add(Activation('relu')) 21 | model.add(MaxPooling2D(pool_size=(2, 2))) 22 | 23 | #the model so far outputs 3D feature maps (height, width, features) 24 | # 然后我们接了两个全连接网络,并以单个神经元和Sigmoid激活结束模型 25 | # 这种选择会产生一个二分类的结果,与这种配置项适应,损失函数选择binary_crossentropy 26 | # this converts our 3D feature maps to 1D feature vectors 27 | model.add(Flatten()) 28 | # 添加隐藏层神经元的数量和激活函数 29 | model.add(Dense(64)) 30 | model.add(Activation('relu')) 31 | model.add(Dropout(0.5)) 32 | model.add(Dense(1)) 33 | model.add(Activation('sigmoid')) 34 | 35 | model.compile(loss='binary_crossentropy', 36 | optimizer='rmsprop', 37 | metrics=['accuracy']) 38 | 39 | # next read data 40 | # 使用 .flow_from_directory() 来从我们的jpgs图片中直接产生数据和标签 41 | # this is the augmentation configuration we will use for training 42 | train_datagen = ImageDataGenerator( 43 | rescale=1./255, 44 | shear_range=0.2, 45 | zoom_range=0.2, 46 | horizontal_flip=True 47 | ) 48 | # this is the augmentation configuration we will use for testing only rescaling 49 | test_datagen = ImageDataGenerator(rescale=1./255) 50 | 51 | # this is a generator that will read pictures found in subfliders 52 | # of 'data/train'. and indefinitely generate batches of augmented image data 53 | train_generator = train_datagen.flow_from_directory( 54 | 'DogsVSCats/train', # this is the target directory 55 | target_size=(150, 150), # all image will be resize to 150*150 56 | batch_size=32, 57 | class_mode='binary' 58 | ) # since we use binary_crossentropy loss,we need binary labels 59 | 60 | # this is a similar generator, for validation data 61 | validation_generator = test_datagen.flow_from_directory( 62 | 'DogsVSCats/valid', # this is the target directory 63 | target_size=(150, 150), # all image will be resize to 150*150 64 | batch_size=32, 65 | class_mode='binary' 66 | ) 67 | 68 | # 然后我们可以用这个生成器来训练网络了。 69 | model.fit_generator( 70 | train_generator, 71 | samples_per_epoch=2000, 72 | nb_epoch=50, 73 | validation_data=validation_generator, 74 | nb_val_samples=800 75 | ) 76 | model.save_weights('first_try.h5') #always save your weights after training or duraing trianing 77 | -------------------------------------------------------------------------------- /KerasLearningNote/KerasCode_2/keras_CNNparctice2.py: -------------------------------------------------------------------------------- 1 | # 载入与模型网络构建 2 | from keras.models import Sequential 3 | from keras.layers import Conv2D, MaxPooling2D 4 | from keras.layers import Activation, Dropout, Flatten, Dense 5 | from keras.preprocessing.image import ImageDataGenerator 6 | import numpy as np 7 | import os 8 | 9 | 10 | def built_model(): 11 | # 载入与模型网络构建 12 | model = Sequential() 13 | model.add(Conv2D(32, (3, 3), input_shape=(150, 150, 3))) 14 | # filter大小为3*3 数量为32个,原始图像大小3,150 150 15 | model.add(Activation('relu')) 16 | model.add(MaxPooling2D(pool_size=(2, 2))) 17 | 18 | model.add(Conv2D(32, (3, 3))) 19 | model.add(Activation('relu')) 20 | model.add(MaxPooling2D(pool_size=(2, 2))) 21 | 22 | model.add(Conv2D(64, (3, 3))) 23 | model.add(Activation('relu')) 24 | model.add(MaxPooling2D(pool_size=(2, 2))) 25 | 26 | model.add(Conv2D(64, (3, 3))) 27 | model.add(Activation('relu')) 28 | model.add(MaxPooling2D(pool_size=(2, 2))) 29 | model.add(Flatten()) 30 | # this converts ours 3D feature maps to 1D feature vector 31 | model.add(Dense(64)) 32 | model.add(Activation('relu')) 33 | model.add(Dropout(0.5)) 34 | model.add(Dense(5)) # 几个分类就几个dense 35 | model.add(Activation('softmax')) # 多分类 36 | # model.compile(loss='binary_corssentropy', 37 | # optimizer='rmsprop', 38 | # metrics=['accuracy']) 39 | 40 | # 优化器rmsprop:除学习率可调整外,建议保持优化器的其他默认参数不变 41 | model.compile(loss='categorical_crossentropy', 42 | optimizer='rmsprop', 43 | metrics=['accuracy']) 44 | 45 | model.summary() 46 | return model 47 | 48 | 49 | def generate_data(): 50 | ''' 51 | flow_from_directory是计算数据的一些属性值,之后再训练阶段直接丢进去这些生成器。 52 | 通过这个函数来准确数据,可以让我们的jpgs图片中直接产生数据和标签 53 | :return: 54 | ''' 55 | train_datagen = ImageDataGenerator( 56 | rescale=1. / 255, 57 | shear_range=0.2, 58 | zoom_range=0.2, 59 | horizontal_flip=True 60 | ) 61 | test_datagen = ImageDataGenerator(rescale=1. / 255) 62 | train_generator = train_datagen.flow_from_directory( 63 | 'data/mytrain', 64 | target_size=(150, 150), # all images will be resized to 150*150 65 | batch_size=32, 66 | class_mode='categorical' # 多分类 67 | ) 68 | 69 | validation_generator = test_datagen.flow_from_directory( 70 | 'data/mytest', 71 | target_size=(150, 150), 72 | batch_size=32, 73 | class_mode='categorical' # 多分类 74 | ) 75 | return train_generator, validation_generator 76 | 77 | 78 | def train_model(model=None): 79 | if model is None: 80 | model = built_model() 81 | model.fit_generator( 82 | train_generator, 83 | # sampels_per_epoch 相当于每个epoch数据量峰值, 84 | # 每个epoch以经过模型的样本数达到samples_per_epoch时,记一个epoch结束 85 | samples_per_epoch=2000, 86 | nb_epoch=50, 87 | validation_data=validation_generator, 88 | nb_val_samples=800 89 | ) 90 | model.save_weights('first_try_animal.h5') 91 | 92 | 93 | if __name__ == '__main__': 94 | train_generator, validation_generator = generate_data() 95 | train_model() 96 | # 当loss出现负数,肯定是之前多分类的标签哪些设置的不对, 97 | -------------------------------------------------------------------------------- /KerasLearningNote/KerasCode_2/keras_DataEnhancement.py: -------------------------------------------------------------------------------- 1 | #_*_coding:utf-8_*_ 2 | ''' 3 | 使用ImageDataGenerator 来生成图片,并将其保存在一个临时文件夹中 4 | 下面感受一下数据提升究竟做了什么事情。 5 | ''' 6 | import os 7 | from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img 8 | 9 | datagen = ImageDataGenerator( 10 | rotation_range=40, # 是一个0~180的度数,用来指定随机选择图片的角度 11 | width_shift_range=0.2, # 水平方向的随机移动程度 12 | height_shift_range=0.2, # 竖直方向的随机移动程度 13 | rescale=1./255, #将在执行其他处理前乘到整个图像上 14 | shear_range=0.2, # 用来进行剪切变换的程度,参考剪切变换 15 | zoom_range=0.2, # 用来进行随机的放大 16 | horizontal_flip=True, # 随机的对图片进行水平翻转,此参数用于水平翻转不影响图片语义的时候 17 | fill_mode='nearest' # 用来指定当需要进行像素填充,如旋转,水平和竖直位移的时候 18 | ) 19 | 20 | pho_path = 'timg.jpg' 21 | img = load_img(pho_path) # this is a PIL image 22 | x = img_to_array(img) # this is a Numpy array with shape (3, 150, 150) 23 | x = x.reshape((1, ) + x.shape) # this is a Numpy array with shape (1, 3, 150, 150) 24 | 25 | if not os.path.exists('preview'): 26 | os.mkdir('preview') 27 | 28 | i = 0 29 | for batch in datagen.flow(x, batch_size=1, 30 | save_to_dir='preview', save_prefix='durant', save_format='jpg'): 31 | i += 1 32 | if i > 20: 33 | break # otherwise the generator would loop indefitely 34 | -------------------------------------------------------------------------------- /KerasLearningNote/KerasCode_2/keras_PredictModel.py: -------------------------------------------------------------------------------- 1 | # 用训练好的模型来预测新的样本 2 | from keras.preprocessing import image 3 | import cv2 4 | import numpy as np 5 | from keras.models import load_model 6 | 7 | 8 | def predict(model, img_path, target_size): 9 | img = cv2.imread(img_path) 10 | if img.shape != target_size: 11 | img = cv2.resize(img, target_size) 12 | # print(img.shape) 13 | x = image.img_to_array(img) 14 | x *= 1. / 255 # 相当于ImageDataGenerator(rescale=1. / 255) 15 | x = np.expand_dims(x, axis=0) # 调整图片维度 16 | preds = model.predict(x) 17 | return preds[0] 18 | 19 | 20 | if __name__ == '__main__': 21 | model_path = 'animal.h5' 22 | model = load_model(model_path) 23 | target_size = (150, 150) 24 | img_path = 'data/test/300.jpg' 25 | res = predict(model, img_path, target_size) 26 | print(res) 27 | -------------------------------------------------------------------------------- /KerasLearningNote/KerasCode_3/keras_ExtractBottleneckFeature.py: -------------------------------------------------------------------------------- 1 | # 提取图片中的 bottleneck 特征 2 | ''' 3 | 步骤:1,载入图片 4 | 2,灌入 pre_model 的权重 5 | 3,得到 bottleneck feature 6 | ''' 7 | from keras.models import Sequential 8 | from keras.layers import Conv2D, MaxPooling2D 9 | from keras.layers import Activation, Dropout, Flatten, Dense 10 | 11 | # 载入图片 图片生成器初始化 12 | from keras.preprocessing.image import ImageDataGenerator 13 | import numpy as np 14 | from keras.applications.vgg16 import VGG16 15 | model = VGG16(include_top=False, weights='imagenet', input_shape=(150, 150, 3)) 16 | print('load model ok') 17 | 18 | datagen = ImageDataGenerator(rescale=1./255) 19 | 20 | # 训练集图像生成器 21 | train_generator = datagen.flow_from_directory( 22 | 'data/mytrain', 23 | target_size=(150, 150), 24 | batch_size=32, 25 | class_mode=None, 26 | shuffle=False 27 | ) 28 | 29 | # 验证集图像生成器 30 | test_generator = datagen.flow_from_directory( 31 | 'data/mytest', 32 | target_size=(150, 150), 33 | batch_size=32, 34 | class_mode=None, 35 | shuffle=False 36 | ) 37 | print('increase image ok') 38 | 39 | # 灌入 pre_model 的权重 40 | WEIGHTS_PATH = '' 41 | model.load_weights('data/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5') 42 | print('load pre model OK ') 43 | 44 | # 得到 bottleneck feature 45 | bottleneck_features_train = model.predict_generator(train_generator, 500) 46 | # 核心,steps是生成器要返回数据的轮数,每个epoch含有500张图片,与model.fit(samples_per_epoch)相对 47 | # 如果这样的话,总共有 500*32个训练样本 48 | np.save(open('bottleneck_features_train.npy', 'w'), bottleneck_features_train) 49 | 50 | bottleneck_features_validation = model.predict_generator(test_generator, 100) 51 | # 与model.fit(nb_val_samples)相对,一个epoch有800张图片,验证集 52 | np.save(open('bottleneck_features_validation.npy', 'w'), bottleneck_features_validation) 53 | -------------------------------------------------------------------------------- /KerasLearningNote/KerasCode_3/keras_FineTuningCompleteCode.py: -------------------------------------------------------------------------------- 1 | from keras.preprocessing.image import ImageDataGenerator 2 | from keras import optimizers 3 | from keras.models import Sequential 4 | from keras.layers import Dropout, Flatten, Dense 5 | from keras.applications.vgg16 import VGG16 6 | from keras.regularizers import l2 7 | from keras.models import Model 8 | 9 | 10 | def build_model(): 11 | # 构建模型 12 | # 载入Model 权重 + 网络 13 | base_model = VGG16(weights='imagenet', include_top=False, input_shape=(150, 150, 3)) 14 | # print(vgg_model.output_shape[1:]) # (4, 4, 512) 15 | 16 | # 网络结构 17 | # build a classifier model to put on top of the convolution model 18 | top_model = Sequential() 19 | top_model.add(Flatten(input_shape=base_model.output_shape[1:])) 20 | # top_model.add(Dense(256, activation='relu', kernel_regularizer=l2(0.001), )) 21 | top_model.add(Dense(256, activation='relu')) 22 | top_model.add(Dropout(0.5)) 23 | # top_model.add(Dense(1, activation='sigmoid')) 24 | top_model.add(Dense(5, activation='softmax')) 25 | 26 | # note that it is neccessary to start with a fully-trained classifier 27 | # including the top classifier, in order to successfully to do fine-tuning 28 | # top_model_weights_path 是上一个模型的权重 29 | # 我们上个模型是将网络的卷积层部分,把全连接以上的部分抛掉,然后在我们自己的训练集 30 | # 和测试集上跑一边,将得到的输出,记录下来,然后基于输出的特征,我们训练一个全连接层 31 | 32 | top_model.load_weights(top_model_weights_path) 33 | 34 | # add the model on top of the convolutional base 35 | # vgg_model.add(top_model) 36 | # 上面代码是需要将 身子 和 头 组装到一起,我们利用函数式组装即可 37 | print(base_model.input, base_model.output) # (?, 150, 150, 3) (None, 4, 4, 512) 38 | vgg_model = Model(inputs=base_model.input, outputs=top_model(base_model.output)) 39 | 40 | # 然后将最后一个卷积块前的卷积层参数冻结 41 | # 普通的模型需要对所有层的weights进行训练调整,但是此处我们只调整VGG16的后面几个卷积层,所以前面的卷积层要冻结起来 42 | for layer in vgg_model.layers[: 15]: # # :25 bug 15层之前都是不需训练的 43 | layer.trainable = False 44 | 45 | # compile the model with a SGD/momentum optimizer 46 | # vgg_model.compile(loss='binary_crossentropy', 47 | # optimizer=optimizers.SGD(lr=1e-4, momentum=0.9), 48 | # metrics=['accuracy']) 49 | vgg_model.compile(loss='categorical_crossentropy', 50 | optimizer=optimizers.SGD(lr=1e-4, momentum=0.9), # 使用一个非常小的lr来微调 51 | metrics=['accuracy']) 52 | vgg_model.summary() 53 | return vgg_model 54 | 55 | 56 | def generate_data(): 57 | # 然后以很低的学习率去训练 58 | # this is the augmentation configuration we will use for training 59 | train_datagen = ImageDataGenerator( 60 | rescale=1. / 255, # 图片像素值为0-255,此处都乘以1/255,调整到0-1之间 61 | shear_range=0.2, # 斜切 62 | zoom_range=0.2, # 方法缩小范围 63 | horizontal_flip=True # 水平翻转 64 | ) 65 | 66 | # this is the augmentation configuration we will use for testing 67 | test_datagen = ImageDataGenerator(rescale=1. / 255) 68 | 69 | # this is a generator that will read pictures found in subfolders of 'data/train' 70 | # and indefinitely generate batches of augmented image data 71 | train_generator = train_datagen.flow_from_directory( 72 | 'data/mytrain', 73 | target_size=(150, 150), 74 | batch_size=batch_size, 75 | class_mode='categorical' # if we use binary_crossentropy loss, we need binary labels 76 | ) 77 | 78 | # this is a similar generator, for validation data 79 | validation_generator = test_datagen.flow_from_directory( 80 | 'data/mytest', 81 | target_size=(150, 150), 82 | batch_size=batch_size, 83 | class_mode='categorical' 84 | ) 85 | # 开始用 train set 来微调模型的参数 86 | print("start to fine-tune my model") 87 | vgg_model.fit_generator( 88 | train_generator, 89 | steps_per_epoch=nb_train_samples // batch_size, 90 | epochs=epochs, 91 | validation_data=validation_generator, 92 | validation_steps=nb_validation_samples // batch_size 93 | ) 94 | 95 | # vgg_model.save_weights('vgg_try.h5') 96 | 97 | 98 | if __name__ == '__main__': 99 | nb_train_samples = 500 100 | nb_validation_samples = 100 101 | epochs = 50 102 | batch_size = 4 103 | top_model_weights_path = 'bottleneck_fc_model.h5' 104 | vgg_model = build_model() 105 | generate_data() 106 | -------------------------------------------------------------------------------- /KerasLearningNote/KerasCode_3/keras_SaveBottleneckFeature.py: -------------------------------------------------------------------------------- 1 | generator = datagen.flow_from_directory( 2 | 'data/train', 3 | target_size=(150, 150), 4 | batch_size=32, 5 | class_mode=None, 6 | shuffle=False 7 | ) 8 | # the predict_generator method returns the output of a model 9 | # given a generator that yields batches of numpy data 10 | bottleneck_feature_train = model.predict_generator(generator, 2000) 11 | # save the output as a Numpy array 12 | np.save(open('bottleneck_features_train.npy', 'w'), bottleneck_feature_train) 13 | 14 | generator = datagen.flow_from_directory( 15 | 'data/validation', 16 | target_size=(150, 150), 17 | batch_size=32, 18 | class_mode=None, 19 | shuffle=False 20 | ) 21 | bottleneck_feature_validaion = model.predict_generator(generator, 2000) 22 | # save the output as a Numpy array 23 | np.save(open('bottleneck_features_validation.npy', 'w'), bottleneck_feature_validaion) 24 | 25 | 26 | train_data = np.load(open('bottleneck_features_train.npy')) 27 | # the features were saved in order, so recreating the labels is easy 28 | train_labels = np.array([0] * 1000 + [1] * 1000) 29 | 30 | validation_data = np.load(open('bottleneck_features_validation.npy')) 31 | validation_labels = np.array([0] * 400 + [1] * 400) 32 | 33 | model = Sequential() 34 | model.add(Flatten(input_shape=train_data.shape[1:])) 35 | model.add(Dense(256, activation='relu')) 36 | model.add(Dropout(0.5)) 37 | model.add(Dense(1, activation='sigmoid')) 38 | 39 | model.compile(optimizer='rmsprop', 40 | loss='binary_crossentropy', 41 | metrics=['accuracy']) 42 | model.fit(train_data, train_labels, nb_epoch=50, batch_size=32, 43 | validation_data=(validation_data, validation_labels)) 44 | model.sample_weights('bottleneck_fc_model.h5') 45 | -------------------------------------------------------------------------------- /KerasLearningNote/KerasCode_3/keras_funeTuning.py: -------------------------------------------------------------------------------- 1 | # (1)导入bottleneck_features数据 2 | train_data = np.load(open('bottleneck_features_train.npy')) 3 | # the features were saved in order, so recreating the labels is easy 4 | train_labels = np.array([0] * 100 + [1] * 100 + [2] * 100 + [3] * 100 + [4] * 96) # matt,打标签 5 | 6 | validation_data = np.load(open('bottleneck_features_validation.npy')) 7 | validation_labels = np.array([0] * 20 + [1] * 20 + [2] * 20 + [3] * 20 + [4] * 16) # matt,打标签 8 | 9 | # (2)设置标签,并规范成Keras默认格式 10 | train_labels = keras.utils.to_categorical(train_labels, 5) 11 | validation_labels = keras.utils.to_categorical(validation_labels, 5) 12 | 13 | # (3)写“小网络”的网络结构 14 | model = Sequential() 15 | #train_data.shape[1:] 16 | model.add(Flatten(input_shape=(4,4,512)))# 4*4*512 17 | model.add(Dense(256, activation='relu')) 18 | model.add(Dropout(0.5)) 19 | #model.add(Dense(1, activation='sigmoid')) # 二分类 20 | model.add(Dense(5, activation='softmax')) # matt,多分类 21 | #model.add(Dense(1)) 22 | #model.add(Dense(5)) 23 | #model.add(Activation('softmax')) 24 | 25 | # (4)设置参数并训练 26 | model.compile(loss='categorical_crossentropy', 27 | # matt,多分类,不是binary_crossentropy 28 | optimizer='rmsprop', 29 | metrics=['accuracy']) 30 | 31 | model.fit(train_data, train_labels, 32 | nb_epoch=50, batch_size=16, 33 | validation_data=(validation_data, validation_labels)) 34 | model.save_weights('bottleneck_fc_model.h5') 35 | -------------------------------------------------------------------------------- /KerasLearningNote/KerasCode_3/keras_parcticeCompleteCode.py: -------------------------------------------------------------------------------- 1 | #_*_coding:utf-8_*_ 2 | from keras.models import Sequential 3 | from keras.layers import Conv2D, MaxPooling2D 4 | from keras.layers import Activation, Dropout, Flatten, Dense 5 | from keras.preprocessing.image import ImageDataGenerator 6 | import numpy as np 7 | from keras.applications.vgg16 import VGG16 8 | import os 9 | 10 | from keras.backend.tensorflow_backend import set_session 11 | import tensorflow as tf 12 | 13 | 14 | os.environ['CUDA_VISIBLE_DEVICES'] = '1,2,3' 15 | config = tf.ConfigProto() 16 | config.gpu_options.per_process_gpu_memory_fraction = 0.4 17 | #config.gpu_options.allow_growth = True 18 | #sess = tf.Session(config=config) 19 | set_session(tf.Session(config=config)) 20 | 21 | 22 | model = VGG16(include_top=False, weights='imagenet', input_shape=(150, 150, 3)) 23 | print('load model ok') 24 | 25 | datagen = ImageDataGenerator(rescale=1./255) 26 | 27 | 28 | train_generator = datagen.flow_from_directory( 29 | '/data/lebron/data/mytrain', 30 | target_size=(150, 150), 31 | batch_size=4, 32 | class_mode=None, 33 | shuffle=False 34 | ) 35 | 36 | 37 | test_generator = datagen.flow_from_directory( 38 | '/data/lebron/data/mytest', 39 | target_size=(150, 150), 40 | batch_size=4, 41 | class_mode=None, 42 | shuffle=False 43 | ) 44 | print('increase image ok') 45 | 46 | model.load_weights('/data/lebron/data/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5') 47 | print('load pre model OK ') 48 | 49 | 50 | bottleneck_features_train = model.predict_generator(train_generator, 125) 51 | 52 | np.save('/data/lebron/bottleneck_features_train.npy', bottleneck_features_train) 53 | 54 | 55 | bottleneck_features_validation = model.predict_generator(test_generator, 25) 56 | 57 | np.save('/data/lebron/bottleneck_features_validation.npy', bottleneck_features_validation) 58 | 59 | print('game over') 60 | -------------------------------------------------------------------------------- /KerasLearningNote/KerasCode_3/keras_parcticeCompleteCode1.py: -------------------------------------------------------------------------------- 1 | # 提取图片中的 bottleneck 特征 2 | ''' 3 | 步骤:1,载入图片 4 | 2,灌入 pre_model 的权重 5 | 3,得到 bottleneck feature 6 | ''' 7 | from keras.models import Sequential 8 | from keras.layers import Conv2D, MaxPooling2D 9 | from keras.layers import Activation, Dropout, Flatten, Dense 10 | 11 | # 载入图片 图片生成器初始化 12 | from keras.preprocessing.image import ImageDataGenerator 13 | import numpy as np 14 | from keras.applications.vgg16 import VGG16 15 | import keras 16 | 17 | def save_bottleneck_features(): 18 | model = VGG16(include_top=False, weights='imagenet', input_shape=(150, 150, 3)) 19 | print('load model ok') 20 | 21 | datagen = ImageDataGenerator(rescale=1./255) 22 | 23 | # 训练集图像生成器 24 | train_generator = datagen.flow_from_directory( 25 | 'data/mytrain', 26 | target_size=(150, 150), 27 | batch_size=16, 28 | class_mode=None, 29 | shuffle=False 30 | ) 31 | 32 | # 验证集图像生成器 33 | test_generator = datagen.flow_from_directory( 34 | 'data/mytest', 35 | target_size=(150, 150), 36 | batch_size=16, 37 | class_mode=None, 38 | shuffle=False 39 | ) 40 | print('increase image ok') 41 | 42 | # 灌入 pre_model 的权重 43 | WEIGHTS_PATH = '' 44 | model.load_weights('data/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5') 45 | print('load pre model OK ') 46 | 47 | # 得到 bottleneck feature 48 | bottleneck_features_train = model.predict_generator(train_generator, 500) 49 | # 核心,steps是生成器要返回数据的轮数,每个epoch含有500张图片,与model.fit(samples_per_epoch)相对 50 | # 如果这样的话,总共有 500*32个训练样本 51 | np.save('bottleneck_features_train.npy', 'w', bottleneck_features_train) 52 | 53 | bottleneck_features_validation = model.predict_generator(test_generator, 100) 54 | # 与model.fit(nb_val_samples)相对,一个epoch有800张图片,验证集 55 | np.save('bottleneck_features_validation.npy', 'w', bottleneck_features_validation) 56 | 57 | 58 | 59 | def train_fine_tune(): 60 | trainfile = 'data/model/train.npy' 61 | testfile = 'data/model/validation.npy' 62 | # (1) 导入 bottleneck features数据 63 | train_data = np.load(trainfile) 64 | print(train_data.shape) # (8000, 4, 4, 512) 65 | # train_data = train_data.reshape(train_data.shape[0], 150, 150, 3) 66 | # the features were saved in order, so recreating the labels is easy 67 | train_labels = np.array( 68 | [0]*100 + [1]*100 + [2]*100 + [3]*100 + [4]*100 69 | ) 70 | validation_data = np.load(testfile) 71 | print(validation_data.shape) # (1432, 4, 4, 512) 72 | validation_labels = np.array( 73 | [0]*20 + [1]*20 + [2]*20 + [3]*20 + [4]*20 74 | ) 75 | 76 | # (2) 设置标签,并规范成Keras默认格式 77 | train_labels = keras.utils.to_categorical(train_labels, 5) 78 | validation_labels = keras.utils.to_categorical(validation_labels, 5) 79 | print(train_labels.shape, validation_labels.shape) # (8000, 5) (1432, 5) 80 | # (3) 写“小网络”的网络结构 81 | model = Sequential() 82 | # train_data.shape[1:] 83 | model.add(Flatten(input_shape=(4, 4, 512))) #4*4*512 84 | model.add(Dense(256, activation='relu')) 85 | model.add(Dropout(0.5)) 86 | # model.add(Dense(1, activation='sigmoid')) # 二分类 87 | model.add(Dense(5, activation='softmax')) # 多分类 88 | 89 | # (4) 设置参数并训练 90 | model.compile(loss='categorical_crossentropy', # 两分类是 binary_crossentropy 91 | optimizer='rmsprop', 92 | metrics=['accuracy']) 93 | 94 | model.fit(train_data, train_labels, 95 | nb_epoch=50, batch_size=16, 96 | validation_data=(validation_data, validation_labels)) 97 | model.save_weights('bottleneck_fc_model.h5') 98 | 99 | if __name__ == '__main__': 100 | # save_bottleneck_features() 101 | train_fine_tune() 102 | # print('over') 103 | -------------------------------------------------------------------------------- /KerasLearningNote/KerasCode_4/keras_FinetuneInceptionV3.py: -------------------------------------------------------------------------------- 1 | from keras.applications.inception_v3 import InceptionV3 2 | from keras.preprocessing import image 3 | from keras.models import Model 4 | from keras.layers import Dense, GlobalAveragePooling2D 5 | from keras import backend as K 6 | 7 | # create the base pre-trained model 8 | base_model = InceptionV3(weights='imagenet', include_top=False) 9 | 10 | # add a global spatial average pooling layer 11 | x = base_model.output 12 | x = GlobalAveragePooling2D()(x) 13 | # let's add a fully-connected layer 14 | x = Dense(1024, activation='relu')(x) 15 | # and a logistic layer -- let's say we have 200 classes 16 | predictions = Dense(200, activation='softmax')(x) 17 | 18 | # this is the model we will train 19 | model = Model(input=base_model.input, output=predictions) 20 | 21 | # first: train only the top layers (which were randomly initialized) 22 | # i.e. freeze all convolutional InceptionV3 layers 23 | for layer in base_model.layers: 24 | layer.trainable = False 25 | 26 | # compile the model (should be done *after* setting layers to non-trainable) 27 | model.compile(optimizer='rmsprop', loss='categorical_crossentropy') 28 | 29 | # train the model on the new data for a few epochs 30 | model.fit_generator(...) 31 | 32 | # at this point, the top layers are well trained and we can start fine-tuning 33 | # convolutional layers from inception V3. We will freeze the bottom N layers 34 | # and train the remaining top layers. 35 | 36 | # let's visualize layer names and layer indices to see how many layers 37 | # we should freeze: 38 | for i, layer in enumerate(base_model.layers): 39 | print(i, layer.name) 40 | 41 | # we chose to train the top 2 inception blocks, i.e. we will freeze 42 | # the first 172 layers and unfreeze the rest: 43 | for layer in model.layers[:172]: 44 | layer.trainable = False 45 | for layer in model.layers[172:]: 46 | layer.trainable = True 47 | 48 | # we need to recompile the model for these modifications to take effect 49 | # we use SGD with a low learning rate 50 | from keras.optimizers import SGD 51 | model.compile(optimizer=SGD(lr=0.0001, momentum=0.9), loss='categorical_crossentropy') 52 | 53 | # we train our model again (this time fine-tuning the top 2 inception blocks 54 | # alongside the top Dense layers 55 | model.fit_generator(...) 56 | -------------------------------------------------------------------------------- /KerasLearningNote/KerasCode_4/keras_MusicTaggerCRNNcode.py: -------------------------------------------------------------------------------- 1 | from keras.applications.music_tagger_crnn import MusicTaggerCRNN 2 | from keras.applications.music_tagger_crnn import preprocess_input, decode_predictions 3 | import numpy as np 4 | 5 | # 1. Tagging 6 | model = MusicTaggerCRNN(weights='msd') 7 | 8 | audio_path = 'audio_file.mp3' 9 | melgram = preprocess_input(audio_path) 10 | melgrams = np.expand_dims(melgram, axis=0) 11 | 12 | preds = model.predict(melgrams) 13 | print('Predicted:') 14 | print(decode_predictions(preds)) 15 | # print: ('Predicted:', [[('rock', 0.097071797), ('pop', 0.042456303), ('alternative', 0.032439161), ('indie', 0.024491295), ('female vocalists', 0.016455274)]]) 16 | 17 | #. 2. Feature extraction 18 | model = MusicTaggerCRNN(weights='msd', include_top=False) 19 | 20 | audio_path = 'audio_file.mp3' 21 | melgram = preprocess_input(audio_path) 22 | melgrams = np.expand_dims(melgram, axis=0) 23 | 24 | feats = model.predict(melgrams) 25 | print('Features:') 26 | print(feats[0, :10]) 27 | # print: ('Features:', [-0.19160545 0.94259131 -0.9991011 0.47644514 -0.19089699 0.99033844 0.1103896 -0.00340496 0.14823607 0.59856361]) 28 | -------------------------------------------------------------------------------- /KerasLearningNote/KerasCode_4/keras_ResNet50Application.py: -------------------------------------------------------------------------------- 1 | # 利用ResNet50网络进行 ImageNet 分类 2 | from keras.applications.resnet50 import ResNet50 3 | from keras.preprocessing import image 4 | from keras.applications.resnet50 import preprocess_input, decode_predictions 5 | import numpy as np 6 | 7 | model = ResNet50(weights='imagenet') 8 | 9 | img_path = 'elephant.jpg' 10 | img = image.load_img(img_path, target_size=(224, 224)) 11 | x = image.img_to_array(img) 12 | x = np.expand_dims(x, axis=0) 13 | x = preprocess_input(x) 14 | 15 | preds = model.predict(x) 16 | # decode the results into a list of tuples(class description, probability) 17 | # one such list for each sample in the batch 18 | print('Predicted:', decode_predictions(preds, top=3)[0]) 19 | ''' 20 | Predicted: [('n01871265', 'tusker', 0.40863296), 21 | ('n02504458', 'African_elephant', 0.36055887), 22 | ('n02504013', 'Indian_elephant', 0.22416794)] 23 | ''' 24 | -------------------------------------------------------------------------------- /KerasLearningNote/KerasCode_4/keras_VGG16Application.py: -------------------------------------------------------------------------------- 1 | # 利用VGG16提取特征 2 | from keras.applications.vgg16 import VGG16 3 | from keras.preprocessing import image 4 | from keras.applications.vgg16 import preprocess_input 5 | import numpy as np 6 | 7 | model = VGG16(weights='imagenet', include_top=False) 8 | 9 | img_path = 'elephant.jpg' 10 | img = image.load_img(img_path, target_size=(224, 224)) 11 | x = image.img_to_array(img) 12 | x = np.expand_dims(x, axis=0) 13 | x = preprocess_input(x) 14 | 15 | features = model.predict(x) 16 | print(features.shape, type(features)) 17 | # (1, 7, 7, 512) 18 | -------------------------------------------------------------------------------- /KerasLearningNote/KerasCode_4/keras_VGG19Application.py: -------------------------------------------------------------------------------- 1 | # 利用VGG19提取特征 2 | from keras.applications.vgg19 import VGG19 3 | from keras.preprocessing import image 4 | from keras.applications.vgg19 import preprocess_input 5 | from keras.models import Model 6 | import numpy as np 7 | 8 | base_model = VGG19(weights='imagenet') 9 | model = Model(inputs=base_model.input, 10 | outputs=base_model.get_layer('block4_pool').output) 11 | 12 | img_path = 'elephant.jpg' 13 | img = image.load_img(img_path, target_size=(224, 224)) 14 | x = image.img_to_array(img) 15 | x = np.expand_dims(x, axis=0) 16 | x = preprocess_input(x) 17 | 18 | block4_pool_features = model.predict(x) 19 | print(block4_pool_features.shape, type(block4_pool_features)) 20 | -------------------------------------------------------------------------------- /KerasLearningNote/KerasCode_4/keras_parcticeCompleteCode.py: -------------------------------------------------------------------------------- 1 | from keras.applications.mobilenet import MobileNet 2 | from keras.models import Sequential 3 | from keras.layers import Conv2D, MaxPooling2D 4 | from keras.layers import Activation, Dropout, Flatten, Dense 5 | from keras.preprocessing.image import ImageDataGenerator 6 | import numpy as np 7 | import os 8 | import keras 9 | import tensorflow as tf 10 | from keras.backend.tensorflow_backend import set_session 11 | 12 | os.environ['CUDA_VISIBLE_DEVICES'] = '1,2' 13 | config = tf.ConfigProto() 14 | config.gpu_options.per_process_gpu_memory_fraction = 0.3 15 | set_session(tf.Session(config=config)) 16 | 17 | 18 | def save_bottleneck_features(): 19 | model = MobileNet(include_top=False, weights='imagenet', input_shape=(150, 150, 3)) 20 | print('load model ok') 21 | datagen = ImageDataGenerator(rescale=1. / 255) 22 | 23 | # train set image generator 24 | train_generator = datagen.flow_from_directory( 25 | '/data/lebron/data/mytrain', 26 | target_size=(150, 150), 27 | batch_size=batch_size, 28 | class_mode=None, 29 | shuffle=False 30 | ) 31 | 32 | # test set image generator 33 | test_generator = datagen.flow_from_directory( 34 | '/data/lebron/data/mytest', 35 | target_size=(150, 150), 36 | batch_size=batch_size, 37 | class_mode=None, 38 | shuffle=False 39 | ) 40 | 41 | # load weight 42 | model.load_weights(WEIGHTS_PATH_NO_TOP) 43 | print('load weight ok') 44 | # get bottleneck feature 45 | bottleneck_features_train = model.predict_generator(train_generator, 10) 46 | np.save(save_train_path, bottleneck_features_train) 47 | 48 | bottleneck_features_validation = model.predict_generator(test_generator, 2) 49 | np.save(save_test_path, bottleneck_features_validation) 50 | 51 | 52 | def train_fine_tune(): 53 | # load bottleneck features 54 | train_data = np.load(save_train_path) 55 | train_labels = np.array( 56 | [0] * 100 + [1] * 100 + [2] * 100 + [3] * 100 + [4] * 100 57 | ) 58 | validation_data = np.load(save_test_path) 59 | validation_labels = np.array( 60 | [0] * 20 + [1] * 20 + [2] * 20 + [3] * 20 + [4] * 20 61 | ) 62 | # set labels 63 | train_labels = keras.utils.to_categorical(train_labels, 5) 64 | validation_labels = keras.utils.to_categorical(validation_labels, 5) 65 | 66 | model = Sequential() 67 | model.add(Flatten(input_shape=train_data.shape[1:])) 68 | model.add(Dense(256, activation='relu')) 69 | model.add(Dropout(0.5)) 70 | model.add(Dense(5, activation='softmax')) 71 | 72 | model.compile(loss='categorical_crossentropy', 73 | optimizer='rmsprop', 74 | metrics=['accuracy']) 75 | 76 | model.fit(train_data, train_labels, 77 | nb_epoch=500, batch_size=25, 78 | validation_data=(validation_data, validation_labels)) 79 | 80 | 81 | if __name__ == '__main__': 82 | WEIGHTS_PATH = '/data/model/mobilenet_1_0_224_tf.h5' 83 | WEIGHTS_PATH_NO_TOP = '/data/model/mobilenet_1_0_224_tf_no_top.h5' 84 | save_train_path = '/data/bottleneck_features_train.npy' 85 | save_test_path = '/data/bottleneck_features_validation.npy' 86 | batch_size = 50 87 | save_bottleneck_features() 88 | train_data = np.load(save_train_path) 89 | validation_data = np.load(save_test_path) 90 | print(train_data.shape, validation_data.shape) 91 | train_fine_tune() 92 | print('game over') 93 | -------------------------------------------------------------------------------- /KerasLearningNote/KerasCode_5/keras_parcticeCNN1.py: -------------------------------------------------------------------------------- 1 | import keras 2 | from keras.models import Sequential 3 | from keras.layers import Dense 4 | from keras.datasets import mnist 5 | from keras.utils import to_categorical 6 | import numpy as np 7 | 8 | (X_train, y_train), (X_test, y_test) = mnist.load_data() 9 | # print(X_train.shape[0]) 10 | X_train, X_test = X_train.reshape(X_train.shape[0], 784), X_test.reshape(X_test.shape[0], 784) 11 | X_train, X_test = X_train.astype('float32'), X_test.astype('float32') 12 | X_train /= 255 13 | X_test /= 255 14 | 15 | y_train, y_test = to_categorical(y_train, num_classes=10), to_categorical(y_test, num_classes=10) 16 | 17 | model = Sequential() 18 | model.add(Dense(units=784, activation='relu', input_dim=784)) 19 | model.add(Dense(units=10, activation='softmax')) 20 | model.summary() 21 | model.compile(loss='categorical_crossentropy', 22 | optimizer='sgd', 23 | metrics=['accuracy']) 24 | 25 | model.fit(X_train, y_train, epochs=5, batch_size=32) 26 | 27 | score = model.evaluate(X_test, y_test, batch_size=128) 28 | print('loss:', score[0]) 29 | print('accu:', score[1]) 30 | 31 | model.predict_classes(X_test, batch_size=128) 32 | -------------------------------------------------------------------------------- /KerasLearningNote/KerasCode_5/keras_parcticeCNN2.py: -------------------------------------------------------------------------------- 1 | from keras.models import Sequential 2 | from keras.layers import Conv2D, Activation, MaxPooling2D, Flatten, Dense 3 | from keras.datasets import cifar10 4 | from keras.utils import to_categorical 5 | import numpy as np 6 | import matplotlib.pyplot as plt 7 | from keras.optimizers import SGD 8 | 9 | (X_train, y_train), (X_test, y_test) = cifar10.load_data() 10 | X_train, X_test = X_train.astype('float32'), X_test.astype('float32') 11 | X_train /= 255.0 12 | X_test /= 255.0 13 | y_train, y_test = to_categorical(y_train, num_classes=10), to_categorical(y_test, num_classes=10) 14 | 15 | learning_rate = 0.001 16 | epoch = 10 17 | decay = learning_rate / epoch 18 | sgd = SGD(lr=learning_rate, momentum=0.9, decay=decay, nesterov=False) 19 | 20 | model = Sequential() 21 | model.add(Conv2D(6, (5, 5), input_shape=(32, 32, 3))) 22 | model.add(Activation('relu')) 23 | model.add(MaxPooling2D(pool_size=(2, 2))) 24 | model.add(Conv2D(16, (5, 5), activation='relu')) 25 | model.add(MaxPooling2D(pool_size=(2, 2))) 26 | model.add(Flatten()) 27 | model.add(Dense(120, activation='relu')) 28 | model.add(Dense(84, activation='relu')) 29 | model.add(Dense(10, activation='softmax')) 30 | model.summary() 31 | model.compile(loss='categorical_crossentropy', 32 | optimizer=sgd, 33 | metrics=['accuracy']) 34 | 35 | history = model.fit(X_train, y_train, 36 | validation_split=0.2, 37 | epochs=20, batch_size=32, verbose=1) 38 | plt.figure(12) 39 | plt.subplot(121) 40 | plt.plot(history.history['acc']) 41 | plt.plot(history.history['val_acc']) 42 | plt.title('Model accuracy') 43 | plt.ylabel('Accuracy') 44 | plt.xlabel('Epochs') 45 | plt.legend(['Train', 'val'], loc='upper left') 46 | plt.subplot(122) 47 | plt.plot(history.history['loss']) 48 | plt.plot(history.history['val_loss']) 49 | plt.title('Model loss') 50 | plt.ylabel('Loss') 51 | plt.xlabel('Epochs') 52 | plt.legend(['Train', 'val'], loc='upper left') 53 | plt.show() 54 | 55 | score = model.evaluate(X_test, y_test, verbose=1) 56 | print(model.metrics_names) 57 | print('loss:', score[0]) 58 | print('accu:', score[1]) 59 | 60 | prediction = model.predict_classes(X_test) 61 | print(prediction[:10]) 62 | -------------------------------------------------------------------------------- /KerasLearningNote/KerasCode_5/keras_parcticeCNN3.py: -------------------------------------------------------------------------------- 1 | from keras.models import Sequential, Input, Model 2 | from keras.layers import Conv2D, Activation, MaxPooling2D, Flatten, Dense 3 | from keras.datasets import imdb 4 | from keras.utils import to_categorical 5 | import numpy as np 6 | import matplotlib.pyplot as plt 7 | from keras.optimizers import SGD 8 | import keras 9 | from keras.layers import Embedding, LSTM 10 | 11 | max_features = 10000 12 | # 该数据库含有IMDB的25000条影评,被标记为正面/负面两种评价,影评已被预处理为词下标构成的序列 13 | # y_train和y_test 序列的标签,是一个二值 list 14 | (X_train, y_train), (X_test, y_test) = imdb.load_data(num_words=max_features) 15 | print(X_train.shape, y_train.shape) # (25000,) (25000,) 16 | 17 | main_input = Input(shape=(100,), dtype='int32', name='main_input') 18 | x = Embedding(output_dim=512, input_dim=10000, input_length=100)(main_input) 19 | lstm_out = LSTM(32)(x) 20 | auxiliary_output = Dense(1, activation='sigmoid', name='aux_output')(lstm_out) 21 | auxiliary_input = Input(shape=(5,), name='aux_input') 22 | x = keras.layers.concatenate([lstm_out, auxiliary_input]) 23 | 24 | x = Dense(64, activation='relu')(x) 25 | x = Dense(64, activation='relu')(x) 26 | x = Dense(64, activation='relu')(x) 27 | 28 | main_output = Dense(1, activation='sigmoid', name='main_output')(x) 29 | model = Model(inputs=[main_input, auxiliary_input], 30 | outputs=[main_output, auxiliary_output]) 31 | 32 | model.compile(optimizer='rmsprop', 33 | loss={'main_output': 'binary_crossentropy', 34 | 'aux_output': 'binary_crossentropy'}, 35 | loss_weights={'main_output': 1, 'aux_output': 0.2}) 36 | 37 | model.fit({'main_input': headline_data, 'aux_input': additional_data}, 38 | {'main_output': headline_labels, 'aux_input': additional_label}, 39 | epochs=50, batch_size=32, verbose=0) 40 | 41 | model.predict({'main_input': headline_data, 'aux_input': additional_data}) 42 | -------------------------------------------------------------------------------- /KerasLearningNote/KerasCode_5/keras_parcticeLSTM.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | from keras.preprocessing import sequence 3 | from keras.models import Sequential 4 | from keras.layers import Dense, Embedding 5 | from keras.layers import LSTM 6 | from keras.datasets import imdb 7 | 8 | max_features = 20000 9 | maxlen = 80 10 | batch_size = 32 11 | print('loading data...') 12 | (x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_features) 13 | print(len(x_train), 'train sequences') # 25000 train sequences 14 | print(len(x_test), 'test sequences') # 25000 test sequences 15 | 16 | print('Pad sequences (samples x time)') 17 | x_train = sequence.pad_sequences(x_train, maxlen=maxlen) 18 | x_test = sequence.pad_sequences(x_test, maxlen=maxlen) 19 | print('x_train shape:', x_train.shape) # x_train shape: (25000, 80) 20 | print('x_test shape:', x_test.shape) # x_test shape: (25000, 80) 21 | 22 | print('Build model...') 23 | model = Sequential() 24 | model.add(Embedding(max_features, 128)) 25 | model.add(LSTM(128, dropout=0.2, recurrent_dropout=0.2)) 26 | model.add(Dense(1, activation='sigmoid')) 27 | 28 | # try using different optimizers and different optimizer configs 29 | model.compile(loss='binary_crossentropy', 30 | optimizer='adam', 31 | metrics=['accuracy']) 32 | print("Train...") 33 | model.fit(x_train, y_train, 34 | batch_size=batch_size, 35 | epochs=15, 36 | validation_data=(x_test, y_test)) 37 | score, acc = model.evaluate(x_test, y_test, 38 | batch_size=batch_size) 39 | print('Test score:', score) 40 | print('Test accuracy:', acc) 41 | -------------------------------------------------------------------------------- /KerasLearningNote/KerasCode_5/keras_recordLossHistory.py: -------------------------------------------------------------------------------- 1 | class LossHistory(keras.callbacks.Callback): 2 | def on_train_begin(self, logs={}): 3 | self.losses = [] 4 | 5 | def on_batch_end(self, batch, logs={}): 6 | self.losses.append(logs.get('loss')) 7 | 8 | model = Sequential() 9 | model.add(Dense(10, input_dim=784, kernel_initializer='uniform')) 10 | model.add(Activation('softmax')) 11 | model.compile(loss='categorical_crossentropy', optimizer='rmsprop') 12 | 13 | history = LossHistory() 14 | model.fit(x_train, y_train, batch_size=128, epochs=20, verbose=0, callbacks=[history]) 15 | 16 | print(history.losses) 17 | # 输出 18 | ''' 19 | [0.66047596406559383, 0.3547245744908703, ..., 0.25953155204159617, 0.25901699725311789] 20 | ''' 21 | 22 | class myCallback(keras.callbacks.Callback): 23 | def on_epoch_end(self, epoch, logs=None): 24 | if (logs.get('acc') > 0.95): 25 | print("\nReached 95% accuracy so cancelling training !") 26 | self.model.stop_training = True 27 | -------------------------------------------------------------------------------- /KerasLearningNote/README.md: -------------------------------------------------------------------------------- 1 | # Keras学习笔记地址 2 | *** 3 | ###### 摘要 4 | > 这个文件夹存放的是这里存放自己学习Keras的相关代码,和学习Keras的博客地址,希望可以帮助到别人 5 | *** 6 | *** 7 | ### 我的Keras使用总结(1)——Keras概述与常见问题整理 8 | - 地址:https://www.cnblogs.com/wj-1314/p/12510887.html 9 | *** 10 | #### 关于此博客的代码与数据在: KerasCode_1 11 | *** 12 | *** 13 | ### 我的Keras使用总结(2)——构建图像分类模型(针对小数据集) 14 | - 地址:https://www.cnblogs.com/wj-1314/p/10036718.html 15 | *** 16 | #### 关于此博客的代码与数据在: KerasCode_1 17 | *** 18 | *** 19 | ### 我的Keras使用总结(3)——利用bottleneck features进行微调预训练模型VGG16 20 | - 地址:https://www.cnblogs.com/wj-1314/p/9579485.html 21 | *** 22 | #### 关于此博客的代码与数据在: KerasCode_3 23 | *** 24 | *** 25 | ### 我的Keras使用总结(4)——Application中五款预训练模型学习及其应用 26 | - 地址:https://www.cnblogs.com/wj-1314/p/12526708.html 27 | *** 28 | #### 关于此博客的代码与数据在: KerasCode_4 29 | *** 30 | *** 31 | ### 我的Keras使用总结(5)——Keras指定显卡且限制显存用量,常见函数的用法及其习题练习 32 | - 地址:https://www.cnblogs.com/wj-1314/p/11846622.html 33 | *** 34 | #### 关于此博客的代码与数据在: KerasCode_5 35 | *** 36 | *** 37 | -------------------------------------------------------------------------------- /KerasLearningNote/traindata10code/README.md: -------------------------------------------------------------------------------- 1 | 这里是训练CIFAR-10 数据,我的模型训练图和代码 2 | -------------------------------------------------------------------------------- /KerasLearningNote/traindata10code/cifar10_trainrecord.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/traindata10code/cifar10_trainrecord.jpg -------------------------------------------------------------------------------- /KerasLearningNote/traindata10code/custom_layers.py: -------------------------------------------------------------------------------- 1 | from keras.engine import Layer, InputSpec 2 | try: 3 | from keras import initializations 4 | except ImportError: 5 | from keras import initializers as initializations 6 | import keras.backend as K 7 | 8 | class Scale(Layer): 9 | '''Custom Layer for DenseNet used for BatchNormalization. 10 | 11 | Learns a set of weights and biases used for scaling the input data. 12 | the output consists simply in an element-wise multiplication of the input 13 | and a sum of a set of constants: 14 | out = in * gamma + beta, 15 | where 'gamma' and 'beta' are the weights and biases larned. 16 | # Arguments 17 | axis: integer, axis along which to normalize in mode 0. For instance, 18 | if your input tensor has shape (samples, channels, rows, cols), 19 | set axis to 1 to normalize per feature map (channels axis). 20 | momentum: momentum in the computation of the 21 | exponential average of the mean and standard deviation 22 | of the data, for feature-wise normalization. 23 | weights: Initialization weights. 24 | List of 2 Numpy arrays, with shapes: 25 | `[(input_shape,), (input_shape,)]` 26 | beta_init: name of initialization function for shift parameter 27 | (see [initializations](../initializations.md)), or alternatively, 28 | Theano/TensorFlow function to use for weights initialization. 29 | This parameter is only relevant if you don't pass a `weights` argument. 30 | gamma_init: name of initialization function for scale parameter (see 31 | [initializations](../initializations.md)), or alternatively, 32 | Theano/TensorFlow function to use for weights initialization. 33 | This parameter is only relevant if you don't pass a `weights` argument. 34 | ''' 35 | def __init__(self, weights=None, axis=-1, momentum = 0.9, beta_init='zero', gamma_init='one', **kwargs): 36 | self.momentum = momentum 37 | self.axis = axis 38 | self.beta_init = initializations.get(beta_init) 39 | self.gamma_init = initializations.get(gamma_init) 40 | self.initial_weights = weights 41 | super(Scale, self).__init__(**kwargs) 42 | 43 | def build(self, input_shape): 44 | self.input_spec = [InputSpec(shape=input_shape)] 45 | shape = (int(input_shape[self.axis]),) 46 | 47 | # Tensorflow >= 1.0.0 compatibility 48 | self.gamma = K.variable(self.gamma_init(shape), name='{}_gamma'.format(self.name)) 49 | self.beta = K.variable(self.beta_init(shape), name='{}_beta'.format(self.name)) 50 | #self.gamma = self.gamma_init(shape, name='{}_gamma'.format(self.name)) 51 | #self.beta = self.beta_init(shape, name='{}_beta'.format(self.name)) 52 | self.trainable_weights = [self.gamma, self.beta] 53 | 54 | if self.initial_weights is not None: 55 | self.set_weights(self.initial_weights) 56 | del self.initial_weights 57 | 58 | def call(self, x, mask=None): 59 | input_shape = self.input_spec[0].shape 60 | broadcast_shape = [1] * len(input_shape) 61 | broadcast_shape[self.axis] = input_shape[self.axis] 62 | 63 | out = K.reshape(self.gamma, broadcast_shape) * x + K.reshape(self.beta, broadcast_shape) 64 | return out 65 | 66 | def get_config(self): 67 | config = {"momentum": self.momentum, "axis": self.axis} 68 | base_config = super(Scale, self).get_config() 69 | return dict(list(base_config.items()) + list(config.items())) -------------------------------------------------------------------------------- /KerasLearningNote/traindata10code/data_10record.txt: -------------------------------------------------------------------------------- 1 | ''' 2 | CIFAR-10分类数据,总共十类,每类6000张图片,总共六万张,训练集5万张,测试集1万张。 3 | ''' 4 | 5 | lenet 6 | 1-3 7 | Epoch 19/1000 8 | 50000/50000 [==============================] - 9s 178us/step - loss: 0.8046 - acc: 0.7253 - val_loss: 1.1495 - val_acc: 0.6432 9 | 10000/10000 [==============================] - 1s 57us/step 10 | loss accuracy is [1.1494989082813263, 0.6432] 11 | all time is 173.52810835838318 12 | 13 | 2-3 14 | Epoch 15/1000 15 | 195/195 [==============================] - 2s 9ms/step - loss: 2.3025 - acc: 0.0978 - val_loss: 2.2995 - val_acc: 0.0949 16 | all time is 31.9277184009552 17 | 18 | zfnet alexnet 28, 32 不行 19 | 20 | 21 | 22 | vgglet 23 | 1-3 24 | Epoch 11/1000 25 | 50000/50000 [==============================] - 73s 1ms/step - loss: 14.5063 - acc: 0.1000 - val_loss: 14.5063 - val_acc: 0.1000 26 | 10000/10000 [==============================] - 3s 311us/step 27 | loss accuracy is [14.506285784912109, 0.1] 28 | all time is 817.1582009792328 29 | 30 | 2-3 31 | Epoch 11/1000 32 | 195/195 [==============================] - 7s 38ms/step - loss: 2.3028 - acc: 0.1006 - val_loss: 2.3026 - val_acc: 0.1000 33 | all time is 87.87029147148132 34 | 35 | resnet(32*32) 36 | 1-3 37 | Epoch 32/1000 38 | 50000/50000 [==============================] - 9s 182us/step - loss: 0.6964 - acc: 0.7640 - val_loss: 1.1186 - val_acc: 0.6618 39 | 10000/10000 [==============================] - 1s 59us/step 40 | loss accuracy is [1.1185993865728379, 0.6618] 41 | all time is 293.341192483902 42 | 43 | 2-3 44 | Epoch 15/1000 45 | 195/195 [==============================] - 22s 111ms/step - loss: 1.7088 - acc: 0.3811 - val_loss: 14.4182 - val_acc: 0.1000 46 | all time is 349.6186113357544 47 | 48 | 49 | denselet 50 | 1-3 51 | Epoch 59/1000 52 | 50000/50000 [==============================] - 28s 560us/step - loss: 0.3970 - acc: 0.8853 - val_loss: 0.6477 - val_acc: 0.8046 53 | 10000/10000 [==============================] - 1s 123us/step 54 | loss accuracy is [0.6477235287189483, 0.8046] 55 | all time is 1661.9704973697662 56 | 57 | 2-3 58 | Epoch 24/1000 59 | 195/195 [==============================] - 3s 15ms/step - loss: 1.5288 - acc: 0.4750 - val_loss: 13.8971 - val_acc: 0.1373 60 | all time is 88.30591678619385 61 | 62 | denselet121 63 | 1-3 64 | Epoch 31/1000 65 | 50000/50000 [==============================] - 406s 8ms/step - loss: 0.0616 - acc: 0.9799 - val_loss: 1.5868 - val_acc: 0.7742 66 | 10000/10000 [==============================] - 16s 2ms/step 67 | loss accuracy is [1.5868328019237146, 0.7742] 68 | all time is 12627.581424236298 69 | 70 | 71 | 2-3 72 | Epoch 13/1000 73 | 195/195 [==============================] - 42s 215ms/step - loss: 1.8589 - acc: 0.3317 - val_loss: 14.5047 - val_acc: 0.1001 74 | all time is 583.7792370319366 75 | 76 | 77 | -------------------------------------------------------------------------------- /KerasLearningNote/traindata10code/densenet121loss_1_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/traindata10code/densenet121loss_1_3.jpg -------------------------------------------------------------------------------- /KerasLearningNote/traindata10code/densenet121loss_2_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/traindata10code/densenet121loss_2_3.jpg -------------------------------------------------------------------------------- /KerasLearningNote/traindata10code/densenetloss_1_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/traindata10code/densenetloss_1_3.jpg -------------------------------------------------------------------------------- /KerasLearningNote/traindata10code/densenetloss_2_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/traindata10code/densenetloss_2_3.jpg -------------------------------------------------------------------------------- /KerasLearningNote/traindata10code/lenetloss_1_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/traindata10code/lenetloss_1_3.jpg -------------------------------------------------------------------------------- /KerasLearningNote/traindata10code/lenetloss_2_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/traindata10code/lenetloss_2_3.jpg -------------------------------------------------------------------------------- /KerasLearningNote/traindata10code/resnetloss_1_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/traindata10code/resnetloss_1_3.jpg -------------------------------------------------------------------------------- /KerasLearningNote/traindata10code/resnetloss_2_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/traindata10code/resnetloss_2_3.jpg -------------------------------------------------------------------------------- /KerasLearningNote/traindata10code/vggnetloss_1_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/traindata10code/vggnetloss_1_3.jpg -------------------------------------------------------------------------------- /KerasLearningNote/traindata10code/vggnetloss_2_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/traindata10code/vggnetloss_2_3.jpg -------------------------------------------------------------------------------- /KerasLearningNote/traindata10code_plus/README.md: -------------------------------------------------------------------------------- 1 | 这里是训练CIFAR-10 数据集,控制变量,对优化器做改变的代码和训练图 2 | -------------------------------------------------------------------------------- /KerasLearningNote/traindata10code_plus/custom_layers.py: -------------------------------------------------------------------------------- 1 | from keras.engine import Layer, InputSpec 2 | try: 3 | from keras import initializations 4 | except ImportError: 5 | from keras import initializers as initializations 6 | import keras.backend as K 7 | 8 | class Scale(Layer): 9 | '''Custom Layer for DenseNet used for BatchNormalization. 10 | 11 | Learns a set of weights and biases used for scaling the input data. 12 | the output consists simply in an element-wise multiplication of the input 13 | and a sum of a set of constants: 14 | out = in * gamma + beta, 15 | where 'gamma' and 'beta' are the weights and biases larned. 16 | # Arguments 17 | axis: integer, axis along which to normalize in mode 0. For instance, 18 | if your input tensor has shape (samples, channels, rows, cols), 19 | set axis to 1 to normalize per feature map (channels axis). 20 | momentum: momentum in the computation of the 21 | exponential average of the mean and standard deviation 22 | of the data, for feature-wise normalization. 23 | weights: Initialization weights. 24 | List of 2 Numpy arrays, with shapes: 25 | `[(input_shape,), (input_shape,)]` 26 | beta_init: name of initialization function for shift parameter 27 | (see [initializations](../initializations.md)), or alternatively, 28 | Theano/TensorFlow function to use for weights initialization. 29 | This parameter is only relevant if you don't pass a `weights` argument. 30 | gamma_init: name of initialization function for scale parameter (see 31 | [initializations](../initializations.md)), or alternatively, 32 | Theano/TensorFlow function to use for weights initialization. 33 | This parameter is only relevant if you don't pass a `weights` argument. 34 | ''' 35 | def __init__(self, weights=None, axis=-1, momentum = 0.9, beta_init='zero', gamma_init='one', **kwargs): 36 | self.momentum = momentum 37 | self.axis = axis 38 | self.beta_init = initializations.get(beta_init) 39 | self.gamma_init = initializations.get(gamma_init) 40 | self.initial_weights = weights 41 | super(Scale, self).__init__(**kwargs) 42 | 43 | def build(self, input_shape): 44 | self.input_spec = [InputSpec(shape=input_shape)] 45 | shape = (int(input_shape[self.axis]),) 46 | 47 | # Tensorflow >= 1.0.0 compatibility 48 | self.gamma = K.variable(self.gamma_init(shape), name='{}_gamma'.format(self.name)) 49 | self.beta = K.variable(self.beta_init(shape), name='{}_beta'.format(self.name)) 50 | #self.gamma = self.gamma_init(shape, name='{}_gamma'.format(self.name)) 51 | #self.beta = self.beta_init(shape, name='{}_beta'.format(self.name)) 52 | self.trainable_weights = [self.gamma, self.beta] 53 | 54 | if self.initial_weights is not None: 55 | self.set_weights(self.initial_weights) 56 | del self.initial_weights 57 | 58 | def call(self, x, mask=None): 59 | input_shape = self.input_spec[0].shape 60 | broadcast_shape = [1] * len(input_shape) 61 | broadcast_shape[self.axis] = input_shape[self.axis] 62 | 63 | out = K.reshape(self.gamma, broadcast_shape) * x + K.reshape(self.beta, broadcast_shape) 64 | return out 65 | 66 | def get_config(self): 67 | config = {"momentum": self.momentum, "axis": self.axis} 68 | base_config = super(Scale, self).get_config() 69 | return dict(list(base_config.items()) + list(config.items())) -------------------------------------------------------------------------------- /KerasLearningNote/traindata10code_plus/data_10record.txt: -------------------------------------------------------------------------------- 1 | ''' 2 | CIFAR-10分类数据,总共十类,每类6000张图片,总共六万张,训练集5万张,测试集1万张。 3 | ''' 4 | lenet 5 | 1-3 6 | Epoch 71/1600 7 | 50000/50000 [==============================] - 5s 106us/step - loss: 0.8629 - acc: 0.7010 - val_loss: 1.0936 - val_acc: 0.6217 8 | 10000/10000 [==============================] - 0s 35us/step 9 | loss accuracy is [1.093623847770691, 0.6217] 10 | all time is 373.96161913871765 11 | 12 | 2-3 13 | Epoch 78/1600 14 | 48/48 [==============================] - 1s 17ms/step - loss: 1.6444 - acc: 0.4167 - val_loss: 1.6675 - val_acc: 0.4022 15 | all time is 68.40058493614197 16 | 17 | vggnet-16 18 | 1-3 19 | Epoch 17/1600 20 | 50000/50000 [==============================] - 46s 922us/step - loss: 0.0134 - acc: 0.9959 - val_loss: 1.3592 - val_acc: 0.7956 21 | 10000/10000 [==============================] - 2s 193us/step 22 | loss accuracy is [1.3591884900569915, 0.7956] 23 | all time is 791.4978938102722 24 | 25 | 2-3 26 | Epoch 104/1600 27 | 48/48 [==============================] - 3s 68ms/step - loss: 0.9820 - acc: 0.6393 - val_loss: 0.9864 - val_acc: 0.6488 28 | all time is 348.91572523117065 29 | 30 | 31 | resnet 32 | 1-3 3 33 | Epoch 21/1600 34 | 50000/50000 [==============================] - 131s 3ms/step - loss: 0.0235 - acc: 0.9921 - val_loss: 1.5494 - val_acc: 0.7323 35 | 10000/10000 [==============================] - 6s 629us/step 36 | loss accuracy is [1.5494370854854584, 0.7323] 37 | all time is 2793.3043842315674 38 | 39 | 2-3 4 40 | Epoch 84/1600 41 | 48/48 [==============================] - 10s 213ms/step - loss: 1.5124 - acc: 0.4629 - val_loss: 1.4609 - val_acc: 0.4673 42 | all time is 874.1019806861877 43 | 44 | densenet-121 45 | 1-3 1 46 | Epoch 19/1600 47 | 50000/50000 [==============================] - 236s 5ms/step - loss: 0.0091 - acc: 0.9976 - val_loss: 1.3340 - val_acc: 0.7400 48 | 10000/10000 [==============================] - 9s 914us/step 49 | loss accuracy is [1.3340413320541382, 0.74] 50 | all time is 4573.859783172607 51 | 52 | 53 | 2-3 2 54 | Epoch 66/1600 55 | 48/48 [==============================] - 16s 327ms/step - loss: 1.3477 - acc: 0.4929 - val_loss: 1.2842 - val_acc: 0.5468 56 | all time is 1138.6342658996582 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /KerasLearningNote/traindata10code_plus/densenetloss121_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/traindata10code_plus/densenetloss121_1.jpg -------------------------------------------------------------------------------- /KerasLearningNote/traindata10code_plus/densenetloss121_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/traindata10code_plus/densenetloss121_2.jpg -------------------------------------------------------------------------------- /KerasLearningNote/traindata10code_plus/densenetloss1_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/traindata10code_plus/densenetloss1_3.jpg -------------------------------------------------------------------------------- /KerasLearningNote/traindata10code_plus/densenetloss2_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/traindata10code_plus/densenetloss2_3.jpg -------------------------------------------------------------------------------- /KerasLearningNote/traindata10code_plus/lenetloss1_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/traindata10code_plus/lenetloss1_3.jpg -------------------------------------------------------------------------------- /KerasLearningNote/traindata10code_plus/lenetloss2_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/traindata10code_plus/lenetloss2_3.jpg -------------------------------------------------------------------------------- /KerasLearningNote/traindata10code_plus/resnetloss1_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/traindata10code_plus/resnetloss1_3.jpg -------------------------------------------------------------------------------- /KerasLearningNote/traindata10code_plus/resnetloss2_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/traindata10code_plus/resnetloss2_3.jpg -------------------------------------------------------------------------------- /KerasLearningNote/traindata10code_plus/train_record.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/traindata10code_plus/train_record.jpg -------------------------------------------------------------------------------- /KerasLearningNote/traindata10code_plus/vggnetloss1_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/traindata10code_plus/vggnetloss1_3.jpg -------------------------------------------------------------------------------- /KerasLearningNote/traindata10code_plus/vggnetloss2_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/traindata10code_plus/vggnetloss2_3.jpg -------------------------------------------------------------------------------- /KerasLearningNote/traindata5code/README.md: -------------------------------------------------------------------------------- 1 | 这是训练自己小样本五分类的代码和训练图 2 | -------------------------------------------------------------------------------- /KerasLearningNote/traindata5code/custom_layers.py: -------------------------------------------------------------------------------- 1 | from keras.engine import Layer, InputSpec 2 | try: 3 | from keras import initializations 4 | except ImportError: 5 | from keras import initializers as initializations 6 | import keras.backend as K 7 | 8 | class Scale(Layer): 9 | '''Custom Layer for DenseNet used for BatchNormalization. 10 | 11 | Learns a set of weights and biases used for scaling the input data. 12 | the output consists simply in an element-wise multiplication of the input 13 | and a sum of a set of constants: 14 | out = in * gamma + beta, 15 | where 'gamma' and 'beta' are the weights and biases larned. 16 | # Arguments 17 | axis: integer, axis along which to normalize in mode 0. For instance, 18 | if your input tensor has shape (samples, channels, rows, cols), 19 | set axis to 1 to normalize per feature map (channels axis). 20 | momentum: momentum in the computation of the 21 | exponential average of the mean and standard deviation 22 | of the data, for feature-wise normalization. 23 | weights: Initialization weights. 24 | List of 2 Numpy arrays, with shapes: 25 | `[(input_shape,), (input_shape,)]` 26 | beta_init: name of initialization function for shift parameter 27 | (see [initializations](../initializations.md)), or alternatively, 28 | Theano/TensorFlow function to use for weights initialization. 29 | This parameter is only relevant if you don't pass a `weights` argument. 30 | gamma_init: name of initialization function for scale parameter (see 31 | [initializations](../initializations.md)), or alternatively, 32 | Theano/TensorFlow function to use for weights initialization. 33 | This parameter is only relevant if you don't pass a `weights` argument. 34 | ''' 35 | def __init__(self, weights=None, axis=-1, momentum = 0.9, beta_init='zero', gamma_init='one', **kwargs): 36 | self.momentum = momentum 37 | self.axis = axis 38 | self.beta_init = initializations.get(beta_init) 39 | self.gamma_init = initializations.get(gamma_init) 40 | self.initial_weights = weights 41 | super(Scale, self).__init__(**kwargs) 42 | 43 | def build(self, input_shape): 44 | self.input_spec = [InputSpec(shape=input_shape)] 45 | shape = (int(input_shape[self.axis]),) 46 | 47 | # Tensorflow >= 1.0.0 compatibility 48 | self.gamma = K.variable(self.gamma_init(shape), name='{}_gamma'.format(self.name)) 49 | self.beta = K.variable(self.beta_init(shape), name='{}_beta'.format(self.name)) 50 | #self.gamma = self.gamma_init(shape, name='{}_gamma'.format(self.name)) 51 | #self.beta = self.beta_init(shape, name='{}_beta'.format(self.name)) 52 | self.trainable_weights = [self.gamma, self.beta] 53 | 54 | if self.initial_weights is not None: 55 | self.set_weights(self.initial_weights) 56 | del self.initial_weights 57 | 58 | def call(self, x, mask=None): 59 | input_shape = self.input_spec[0].shape 60 | broadcast_shape = [1] * len(input_shape) 61 | broadcast_shape[self.axis] = input_shape[self.axis] 62 | 63 | out = K.reshape(self.gamma, broadcast_shape) * x + K.reshape(self.beta, broadcast_shape) 64 | return out 65 | 66 | def get_config(self): 67 | config = {"momentum": self.momentum, "axis": self.axis} 68 | base_config = super(Scale, self).get_config() 69 | return dict(list(base_config.items()) + list(config.items())) -------------------------------------------------------------------------------- /KerasLearningNote/traindata5code/data5_trainrecord.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/traindata5code/data5_trainrecord.jpg -------------------------------------------------------------------------------- /KerasLearningNote/traindata5code/data5plus_trainrecord.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeBron-Jian/DeepLearningNote/f9b0018e0a739fad41255b24828ac9c96ad30ceb/KerasLearningNote/traindata5code/data5plus_trainrecord.jpg -------------------------------------------------------------------------------- /KerasLearningNote/traindata5code/data_5record.txt: -------------------------------------------------------------------------------- 1 | ''' 2 | 五分类数据,总共五类,每类100张图片,总共五百张 3 | ''' 4 | 5 | lenet 6 | 1-3 7 | Epoch 73/100 8 | 320/320 [==============================] - 0s 86us/step - loss: 0.0011 - acc: 1.0000 - val_loss: 0.0704 - val_acc: 0.9750 9 | 100/100 [==============================] - 0s 51us/step 10 | loss accuracy is [0.171062770995195, 0.97] 11 | all time is 9.553351163864136 12 | 13 | 2-3 14 | Epoch 77/1000 15 | 2/2 [==============================] - 1s 372ms/step - loss: 0.6103 - acc: 0.8281 - val_loss: 0.6059 - val_acc: 0.7400 16 | all time is 64.24087381362915 17 | 18 | alexnet 19 | 1-3 20 | Epoch 11/1000 21 | 320/320 [==============================] - 1s 2ms/step - loss: 12.4412 - acc: 0.2281 - val_loss: 13.4989 - val_acc: 0.1625 22 | 100/100 [==============================] - 0s 1ms/step 23 | loss accuracy is [13.861561965942382, 0.14] 24 | all time is 16.837616443634033 25 | 2-3 26 | Epoch 11/1000 27 | 2/2 [==============================] - 1s 457ms/step - loss: 13.5996 - acc: 0.1562 - val_loss: 12.8945 - val_acc: 0.2000 28 | all time is 18.4017596244812 29 | 30 | zfnet 31 | 1-3 32 | Epoch 11/1000 33 | 320/320 [==============================] - 1s 2ms/step - loss: 12.4412 - acc: 0.2281 - val_loss: 13.4989 - val_acc: 0.1625 34 | 100/100 [==============================] - 0s 1ms/step 35 | loss accuracy is [13.861561965942382, 0.14] 36 | all time is 16.515499591827393 37 | 38 | 2-3 39 | Epoch 11/1000 40 | 2/2 [==============================] - 1s 456ms/step - loss: 13.0960 - acc: 0.1875 - val_loss: 12.8945 - val_acc: 0.2000 41 | all time is 18.10798192024231 42 | 43 | vgglet 44 | 1-3 45 | Epoch 11/1000 46 | 320/320 [==============================] - 3s 10ms/step - loss: 12.6930 - acc: 0.2125 - val_loss: 13.2974 - val_acc: 0.1750 47 | 100/100 [==============================] - 1s 5ms/step 48 | loss accuracy is [13.21683853149414, 0.18] 49 | all time is 47.34882164001465 50 | 51 | 2-3 52 | Epoch 11/1000 53 | 2/2 [==============================] - 2s 920ms/step - loss: 12.5923 - acc: 0.2188 - val_loss: 12.8945 - val_acc: 0.2000 54 | all time is 32.260655879974365 55 | 56 | resnet: 57 | 1-3 58 | Epoch 11/1000 59 | 320/320 [==============================] - 24s 76ms/step - loss: 12.6930 - acc: 0.2125 - val_loss: 13.2974 - val_acc: 0.1750 60 | 100/100 [==============================] - 3s 26ms/step 61 | loss accuracy is [13.216838226318359, 0.18] 62 | all time is 293.7246935367584 63 | 64 | 2-3 65 | Epoch 21/1000 66 | 10/10 [==============================] - 9s 901ms/step - loss: 12.4915 - acc: 0.2250 - val_loss: 13.0429 - val_acc: 0.1908 67 | all time is 214.39699840545654 68 | 69 | densenet: 70 | 1-3 71 | Epoch 51/1000 72 | 320/320 [==============================] - 2s 5ms/step - loss: 0.0884 - acc: 0.9969 - val_loss: 0.1068 - val_acc: 0.9875 73 | 100/100 [==============================] - 0s 3ms/step 74 | loss accuracy is [0.0701959864795208, 0.99] 75 | all time is 108.43286085128784 76 | 77 | 2-3 78 | Epoch 55/1000 79 | 10/10 [==============================] - 1s 99ms/step - loss: 0.3198 - acc: 0.9125 - val_loss: 0.3185 - val_acc: 0.9423 80 | all time is 72.38848447799683 81 | 82 | densenet-121: 83 | 1-3 84 | Epoch 23/1000 85 | 320/320 [==============================] - 9s 28ms/step - loss: 0.0236 - acc: 0.9906 - val_loss: 0.4737 - val_acc: 0.8375 86 | 100/100 [==============================] - 1s 11ms/step 87 | loss accuracy is [0.2767530243285, 0.86] 88 | all time is 265.9778869152069 89 | 90 | 2-3 91 | Epoch 13/1000 92 | 10/10 [==============================] - 3s 325ms/step - loss: 0.1917 - acc: 0.9250 - val_loss: 6.2146 - val_acc: 0.4934 93 | all time is 99.56758093833923 -------------------------------------------------------------------------------- /PyTorchLearningNote/PyTorchCode_1/pytorch_practice1.py: -------------------------------------------------------------------------------- 1 | # coding:utf-8 2 | import torch 3 | 4 | batch_n = 100 5 | hidden_layer = 100 6 | input_data = 1000 7 | output_data = 10 8 | 9 | x = torch.randn(batch_n, input_data) 10 | y = torch.randn(batch_n, output_data) 11 | 12 | w1 = torch.randn(input_data, hidden_layer) 13 | w2 = torch.randn(hidden_layer, output_data) 14 | 15 | epoch_n = 20 16 | learning_rate = 1e-6 17 | 18 | for epoch in range(epoch_n): 19 | h1 = x.mm(w1) # 100*1000 20 | h1 = h1.clamp(min=0) 21 | y_pred = h1.mm(w2) # 100*10 22 | # print(y_pred) 23 | 24 | loss = (y_pred - y).pow(2).sum() 25 | print("Epoch:{} , Loss:{:.4f}".format(epoch, loss)) 26 | 27 | gray_y_pred = 2 * (y_pred - y) 28 | gray_w2 = h1.t().mm(gray_y_pred) 29 | 30 | grad_h = gray_y_pred.clone() 31 | grad_h = grad_h.mm(w2.t()) 32 | grad_h.clamp_(min=0) 33 | grad_w1 = x.t().mm(grad_h) 34 | 35 | w1 -= learning_rate * grad_w1 36 | w2 -= learning_rate * gray_w2 37 | -------------------------------------------------------------------------------- /PyTorchLearningNote/PyTorchCode_1/pytorch_practice2.py: -------------------------------------------------------------------------------- 1 | #_*_coding:utf-8_*_ 2 | import torch 3 | from torch.autograd import Variable 4 | 5 | # 批量输入的数据量 6 | batch_n = 100 7 | # 通过隐藏层后输出的特征数 8 | hidden_layer = 100 9 | # 输入数据的特征个数 10 | input_data = 1000 11 | # 最后输出的分类结果数 12 | output_data = 10 13 | 14 | x = Variable(torch.randn(batch_n , input_data) , requires_grad = False) 15 | y = Variable(torch.randn(batch_n , output_data) , requires_grad = False) 16 | 17 | w1 = Variable(torch.randn(input_data,hidden_layer),requires_grad = True) 18 | w2 = Variable(torch.randn(hidden_layer,output_data),requires_grad = True) 19 | 20 | # 训练次数设置为20 21 | epoch_n = 20 22 | # 将学习效率设置为0.000001 23 | learning_rate = 1e-6 24 | 25 | for epoch in range(epoch_n): 26 | 27 | # y_pred = x.mm(w1).clamp(min= 0 ).mm(w2) 28 | y_pred = model(x, w1, w2) 29 | loss = (y_pred - y).pow(2).sum() 30 | print("Epoch:{} , Loss:{:.4f}".format(epoch, loss.data[0])) 31 | 32 | loss.backward() 33 | w1.data -= learning_rate * w1.grad.data 34 | w2.data -= learning_rate * w2.grad.data 35 | 36 | w1.grad.data.zero_() 37 | w2.grad.data.zero_() 38 | -------------------------------------------------------------------------------- /PyTorchLearningNote/PyTorchCode_2/pytorch_SetUpModel.py: -------------------------------------------------------------------------------- 1 | #模型搭建和参数优化 2 | # 在顺利完成数据装载后,我们可以开始编写卷积神经网络模型的搭建和参数优化的代码 3 | #卷积层使用torch.nn.Conv2d类来搭建 4 | # 激活层使用torch.nn.ReLU 类方法来搭建 5 | # 池化层使用torch.nn.MaxPool2d类方法来搭建 6 | # 全连接层使用 torch.nn.Linear 类方法来搭建 7 | 8 | class Model(torch.nn.Module): 9 | def __init__(self): 10 | super(Model,self).__init__() 11 | self.conv1 = torch.nn.Sequential( 12 | torch.nn.Conv2d(1,64,kernel_size=3,stride=1,padding=1), 13 | torch.nn.ReLU(), 14 | torch.nn.Conv2d(64,128,kernel_size=3,stride=1,padding=1), 15 | torch.nn.ReLU(), 16 | torch.nn.MaxPool2d(stride=2,kernel_size=2)) 17 | 18 | self.dense = torch.nn.Sequential( 19 | torch.nn.Linear(14*14*128,1024), 20 | torch.nn.ReLU(), 21 | torch.nn.Dropout(p = 0.5), 22 | torch.nn.Linear(1024,10) 23 | ) 24 | 25 | def forward(self, x): 26 | x = self.conv1(x) 27 | x = x.view(-1,14*14*128) 28 | x = self.dense(x) 29 | return x 30 | -------------------------------------------------------------------------------- /PyTorchLearningNote/PyTorchCode_2/pytorch_practice1.py: -------------------------------------------------------------------------------- 1 | #_*_coding:utf-8_*_ 2 | import torch 3 | from torch.autograd import Variable 4 | 5 | # 批量输入的数据量 6 | batch_n = 100 7 | # 通过隐藏层后输出的特征数 8 | hidden_layer = 100 9 | # 输入数据的特征个数 10 | input_data = 1000 11 | # 最后输出的分类结果数 12 | output_data = 10 13 | 14 | x = Variable(torch.randn(batch_n , input_data) , requires_grad = False) 15 | y = Variable(torch.randn(batch_n , output_data) , requires_grad = False) 16 | 17 | models = torch.nn.Sequential( 18 | # 首先通过其完成从输入层到隐藏层的线性变换 19 | torch.nn.Linear(input_data,hidden_layer), 20 | # 经过激活函数 21 | torch.nn.ReLU(), 22 | # 最后完成从隐藏层到输出层的线性变换 23 | torch.nn.Linear(hidden_layer,output_data) 24 | ) 25 | print(models) 26 | -------------------------------------------------------------------------------- /PyTorchLearningNote/PyTorchCode_2/pytorch_practiceByOrderDict.py: -------------------------------------------------------------------------------- 1 | #_*_coding:utf-8_*_ 2 | import torch 3 | from torch.autograd import Variable 4 | from collections import OrderedDict 5 | 6 | # 批量输入的数据量 7 | batch_n = 100 8 | # 通过隐藏层后输出的特征数 9 | hidden_layer = 100 10 | # 输入数据的特征个数 11 | input_data = 1000 12 | # 最后输出的分类结果数 13 | output_data = 10 14 | 15 | models = torch.nn.Sequential(OrderedDict([ 16 | ("Linel",torch.nn.Linear(input_data,hidden_layer)), 17 | ("ReLU1",torch.nn.ReLU()), 18 | ("Line2",torch.nn.Linear(hidden_layer,output_data)) 19 | ]) 20 | ) 21 | print(models) 22 | -------------------------------------------------------------------------------- /PyTorchLearningNote/PyTorchCode_2/pytorch_practiceCompleteCode.py: -------------------------------------------------------------------------------- 1 | #_*_coding:utf-8_*_ 2 | import torch 3 | from torch.autograd import Variable 4 | 5 | # 批量输入的数据量 6 | batch_n = 100 7 | # 通过隐藏层后输出的特征数 8 | hidden_layer = 100 9 | # 输入数据的特征个数 10 | input_data = 1000 11 | # 最后输出的分类结果数 12 | output_data = 10 13 | 14 | x = Variable(torch.randn(batch_n , input_data) , requires_grad = False) 15 | y = Variable(torch.randn(batch_n , output_data) , requires_grad = False) 16 | 17 | models = torch.nn.Sequential( 18 | # 首先通过其完成从输入层到隐藏层的线性变换 19 | torch.nn.Linear(input_data,hidden_layer), 20 | # 经过激活函数 21 | torch.nn.ReLU(), 22 | # 最后完成从隐藏层到输出层的线性变换 23 | torch.nn.Linear(hidden_layer,output_data) 24 | ) 25 | # print(models) 26 | 27 | epoch_n = 10000 28 | learning_rate = 1e-4 29 | loss_fn = torch.nn.MSELoss() 30 | 31 | for epoch in range(epoch_n): 32 | y_pred = models(x) 33 | loss = loss_fn(y_pred,y) 34 | if epoch%1000 == 0: 35 | print("Epoch:{},Loss:{:.4f}".format(epoch,loss.data[0])) 36 | models.zero_grad() 37 | 38 | loss.backward() 39 | 40 | for param in models.parameters(): 41 | param.data -= param.grad.data*learning_rate 42 | -------------------------------------------------------------------------------- /PyTorchLearningNote/PyTorchCode_2/pytorch_practiceCompleteCode2.py: -------------------------------------------------------------------------------- 1 | #_*_coding:utf-8_*_ 2 | import torch 3 | from torch.autograd import Variable 4 | 5 | # 批量输入的数据量 6 | batch_n = 100 7 | # 通过隐藏层后输出的特征数 8 | hidden_layer = 100 9 | # 输入数据的特征个数 10 | input_data = 1000 11 | # 最后输出的分类结果数 12 | output_data = 10 13 | 14 | x = Variable(torch.randn(batch_n , input_data) , requires_grad = False) 15 | y = Variable(torch.randn(batch_n , output_data) , requires_grad = False) 16 | 17 | models = torch.nn.Sequential( 18 | # 首先通过其完成从输入层到隐藏层的线性变换 19 | torch.nn.Linear(input_data,hidden_layer), 20 | # 经过激活函数 21 | torch.nn.ReLU(), 22 | # 最后完成从隐藏层到输出层的线性变换 23 | torch.nn.Linear(hidden_layer,output_data) 24 | ) 25 | # print(models) 26 | 27 | epoch_n = 20 28 | learning_rate = 1e-4 29 | loss_fn = torch.nn.MSELoss() 30 | 31 | optimzer = torch.optim.Adam(models.parameters(),lr = learning_rate) 32 | 33 | #进行模型训练 34 | for epoch in range(epoch_n): 35 | y_pred = models(x) 36 | loss = loss_fn(y_pred,y) 37 | print("Epoch:{}, Loss:{:.4f}".format(epoch, loss.data[0])) 38 | optimzer.zero_grad() 39 | 40 | loss.backward() 41 | 42 | #进行梯度更新 43 | optimzer.step() 44 | -------------------------------------------------------------------------------- /PyTorchLearningNote/PyTorchCode_3/pytorch_practiceCompleteCode2.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | import torchvision.datasets as normal_datasets 4 | import torchvision.transforms as transforms 5 | from torch.autograd import Variable 6 | 7 | num_epochs = 5 8 | batch_size = 100 9 | learning_rate = 0.001 10 | 11 | 12 | # 将数据处理成Variable, 如果有GPU, 可以转成cuda形式 13 | def get_variable(x): 14 | x = Variable(x) 15 | return x.cuda() if torch.cuda.is_available() else x 16 | 17 | 18 | # 从torchvision.datasets中加载一些常用数据集 19 | train_dataset = normal_datasets.MNIST( 20 | root='./mnist/', # 数据集保存路径 21 | train=True, # 是否作为训练集 22 | transform=transforms.ToTensor(), # 数据如何处理, 可以自己自定义 23 | download=True) # 路径下没有的话, 可以下载 24 | 25 | # 见数据加载器和batch 26 | test_dataset = normal_datasets.MNIST(root='./mnist/', 27 | train=False, 28 | transform=transforms.ToTensor()) 29 | 30 | train_loader = torch.utils.data.DataLoader(dataset=train_dataset, 31 | batch_size=batch_size, 32 | shuffle=True) 33 | 34 | test_loader = torch.utils.data.DataLoader(dataset=test_dataset, 35 | batch_size=batch_size, 36 | shuffle=False) 37 | 38 | 39 | # 两层卷积 40 | class CNN(nn.Module): 41 | def __init__(self): 42 | super(CNN, self).__init__() 43 | # 使用序列工具快速构建 44 | self.conv1 = nn.Sequential( 45 | nn.Conv2d(1, 16, kernel_size=5, padding=2), 46 | nn.BatchNorm2d(16), 47 | nn.ReLU(), 48 | nn.MaxPool2d(2)) 49 | self.conv2 = nn.Sequential( 50 | nn.Conv2d(16, 32, kernel_size=5, padding=2), 51 | nn.BatchNorm2d(32), 52 | nn.ReLU(), 53 | nn.MaxPool2d(2)) 54 | self.fc = nn.Linear(7 * 7 * 32, 10) 55 | 56 | def forward(self, x): 57 | out = self.conv1(x) 58 | out = self.conv2(out) 59 | out = out.view(out.size(0), -1) # reshape 60 | out = self.fc(out) 61 | return out 62 | 63 | 64 | cnn = CNN() 65 | if torch.cuda.is_available(): 66 | cnn = cnn.cuda() 67 | 68 | # 选择损失函数和优化方法 69 | loss_func = nn.CrossEntropyLoss() 70 | optimizer = torch.optim.Adam(cnn.parameters(), lr=learning_rate) 71 | 72 | for epoch in range(num_epochs): 73 | for i, (images, labels) in enumerate(train_loader): 74 | images = get_variable(images) 75 | labels = get_variable(labels) 76 | 77 | outputs = cnn(images) 78 | loss = loss_func(outputs, labels) 79 | optimizer.zero_grad() 80 | loss.backward() 81 | optimizer.step() 82 | 83 | if (i + 1) % 100 == 0: 84 | print('Epoch [%d/%d], Iter [%d/%d] Loss: %.4f' 85 | % (epoch + 1, num_epochs, i + 1, len(train_dataset) // batch_size, loss.item())) 86 | 87 | 88 | # Save the Trained Model 89 | torch.save(cnn.state_dict(), 'cnn.pkl') 90 | -------------------------------------------------------------------------------- /PyTorchLearningNote/PyTorchCode_3/pytorch_practiceCompleteCode3.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import cv2 3 | import torch 4 | import torchvision 5 | from torchvision import datasets 6 | from torchvision import transforms 7 | from torch.autograd import Variable 8 | 9 | transform = transforms.Compose([transforms.ToTensor(), 10 | transforms.Normalize(mean=[0.5,0.5,0.5],std=[0.5,0.5,0.5])]) 11 | 12 | 13 | data_test = datasets.MNIST(root="./data/", 14 | transform = transform, 15 | train = False) 16 | 17 | 18 | data_loader_test = torch.utils.data.DataLoader(dataset =data_test, 19 | batch_size = 4, 20 | shuffle = True) 21 | 22 | 23 | #模型搭建和参数优化 24 | # 在顺利完成数据装载后,我们可以开始编写卷积神经网络模型的搭建和参数优化的代码 25 | #卷积层使用torch.nn.Conv2d类来搭建 26 | # 激活层使用torch.nn.ReLU 类方法来搭建 27 | # 池化层使用torch.nn.MaxPool2d类方法来搭建 28 | # 全连接层使用 torch.nn.Linear 类方法来搭建 29 | 30 | class Model(torch.nn.Module): 31 | def __init__(self): 32 | super(Model,self).__init__() 33 | self.conv1 = torch.nn.Sequential( 34 | torch.nn.Conv2d(1,64,kernel_size=3,stride=1,padding=1), 35 | torch.nn.ReLU(), 36 | torch.nn.Conv2d(64,128,kernel_size=3,stride=1,padding=1), 37 | torch.nn.ReLU(), 38 | torch.nn.MaxPool2d(stride=2,kernel_size=2)) 39 | 40 | self.dense = torch.nn.Sequential( 41 | torch.nn.Linear(14*14*128,1024), 42 | torch.nn.ReLU(), 43 | torch.nn.Dropout(p = 0.5), 44 | torch.nn.Linear(1024,10) 45 | ) 46 | 47 | # 我们通过继承torch.nn.Modeule来构造网络,因为手写数字 48 | # 识别比较简单,我们只是用了两个卷积层,一个最大池化层,两个全连接层。 49 | # 在向前传播过程中进行x.view(-1, 14 * 14 * 128) 50 | # 对参数实现扁平化。最后通过自己self.dense定义的全连接层进行最后的分类 51 | def forward(self, x): 52 | x = self.conv1(x) 53 | x = x.view(-1,14*14*128) 54 | x = self.dense(x) 55 | return x 56 | 57 | 58 | # 在编写完搭建卷积神经网络模型的代码后,我们可以对模型进行训练和参数进行优化了 59 | # 首先 定义在训练之前使用哪种损失函数和优化函数 60 | # 下面定义了计算损失值的损失函数使用的是交叉熵 61 | # 优化函数使用的额是Adam自适应优化算法 62 | model = Model() 63 | # 将所有的模型参数移动到GPU上 64 | if torch.cuda.is_available(): 65 | model.cuda() 66 | cost = torch.nn.CrossEntropyLoss() 67 | optimizer = torch.optim.Adam(model.parameters()) 68 | # print(model) 69 | 70 | # 卷积神经网络模型进行模型训练和参数优化的代码 71 | n_epochs = 5 72 | 73 | X_test,y_test = next(iter(data_loader_test)) 74 | inputs = Variable(X_test) 75 | pred = model(inputs) 76 | _,pred = torch.max(pred,1) 77 | 78 | print("Predict Label is:",(i for i in pred)) 79 | print("Real Label is :",[i for i in y_test]) 80 | 81 | img = torchvision.utils.make_grid(X_test) 82 | img = img.numpy().transpose(1,2,0) 83 | 84 | std = [0.5,0.5,0.5] 85 | mean = [0.5,0.5,0.5] 86 | img = img*std +mean 87 | cv2.imshow('win',img) 88 | key_pressed=cv2.waitKey(0) 89 | -------------------------------------------------------------------------------- /PyTorchLearningNote/README.md: -------------------------------------------------------------------------------- 1 | # PyTorch学习笔记地址 2 | *** 3 | ###### 摘要 4 | > 这个文件夹存放的是这里存放自己学习PyTorch的相关代码,和学习PyTorch的博客地址,希望可以帮助到别人 5 | ##### 注意:四和五的博客还没有发,还没有整理好。 6 | *** 7 | *** 8 | ### 深度学习之PyTorch实战(1)——基础学习及搭建环境 9 | - 地址:https://www.cnblogs.com/wj-1314/p/9830950.html 10 | *** 11 | #### 关于此博客的代码与数据在: PyTorchCode_1 12 | *** 13 | *** 14 | ### 深度学习之PyTorch实战(2)——神经网络模型搭建和参数优化 15 | - 地址:https://www.cnblogs.com/wj-1314/p/9838866.html 16 | *** 17 | #### 关于此博客的代码与数据在: PyTorchCode_1 18 | *** 19 | *** 20 | ### 深度学习之PyTorch实战(3)——实战手写数字识别 21 | - 地址:https://www.cnblogs.com/wj-1314/p/9842719.html 22 | *** 23 | #### 关于此博客的代码与数据在: PyTorchCode_3 24 | *** 25 | *** 26 | ### 深度学习之PyTorch实战(4)——迁移学习 27 | - 地址:https://www.cnblogs.com/wj-1314/p/9849892.html 28 | *** 29 | #### 关于此博客的代码与数据在: PyTorchCode_4 30 | *** 31 | *** 32 | ### 深度学习之PyTorch实战(5)——图像风格迁移实战 33 | - 地址:https://www.cnblogs.com/wj-1314/p/9959807.html 34 | *** 35 | #### 关于此博客的代码与数据在: PyTorchCode_5 36 | *** 37 | *** 38 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DeepLearningNote 2 | ## this is all of my code and data with my deep learning note 3 | 包括深度学习论文翻译学习笔记,Keras学习笔记,pytorch学习笔记,tensorflow学习笔记等等 4 | *** 5 | *** 6 | ## DeepLearningObjectDetection 文件夹注解 7 | - 此文件夹为深度学习论文翻译相关博客 8 | - 其中包括分类论文,检测论文等 9 | *** 10 | ## DeepLearningPapersTranslation 文件夹注解 11 | - 此文件为目标检测相关博客及其代码 12 | - 目前包括SSD和YOLO的实现代码,其中SSD是使用Keras实现,YOLO使用Pytroch实现的 13 | *** 14 | ## KerasLearningNote 文件夹注解 15 | - 此文件夹为Keras相关博客及其Keras实现的代码 16 | - 其中包括Keras的基本使用代码,Keras实现的大型网络 17 | - **个人目前对自己完成的Keras版本大型网络(包括:VGGNet16, ResNet,DenseNet 等) 进行一个控制变量的性能比较** 18 | *** 19 | ## PyTorchLearningNote 文件夹注解 20 | - 此文件夹为PyTorch相关博客及其PyTorch实现的代码 21 | - 其中包括PyTorch的基本使用代码,PyTorch实现的大型网络 22 | - **目前做的笔记均是一些Pytorch的基础** 23 | *** 24 | ## TensorflowLearningNote 文件夹注解 25 | - 此文件夹为Tensorflow相关博客及其Tensorflow实现的代码 26 | - 其中包括Tensorflow的基本使用代码,Tensorflow实现的大型网络 27 | - **个人将自己实现的很多大型网络,包括 LeNet,AlexNet,VGGNet,InceptionNet,ResNet,DenseNet,SENet,MobileNet等都放在这个文件夹** 28 | *** 29 | ## BasicMathematicsForArtificialIntelligence 文件夹注解 30 | - 此文件夹为我整理的数学基础相关博客 31 | - 其中包括高等代数基础,线性代数基础,概率论基础等等 32 | *** 33 | -------------------------------------------------------------------------------- /TensorflowLearningNote/DenseNet_code_folder/custom_layers.py: -------------------------------------------------------------------------------- 1 | from keras.engine import Layer, InputSpec 2 | try: 3 | from keras import initializations 4 | except ImportError: 5 | from keras import initializers as initializations 6 | import keras.backend as K 7 | 8 | class Scale(Layer): 9 | '''Custom Layer for DenseNet used for BatchNormalization. 10 | 11 | Learns a set of weights and biases used for scaling the input data. 12 | the output consists simply in an element-wise multiplication of the input 13 | and a sum of a set of constants: 14 | out = in * gamma + beta, 15 | where 'gamma' and 'beta' are the weights and biases larned. 16 | # Arguments 17 | axis: integer, axis along which to normalize in mode 0. For instance, 18 | if your input tensor has shape (samples, channels, rows, cols), 19 | set axis to 1 to normalize per feature map (channels axis). 20 | momentum: momentum in the computation of the 21 | exponential average of the mean and standard deviation 22 | of the data, for feature-wise normalization. 23 | weights: Initialization weights. 24 | List of 2 Numpy arrays, with shapes: 25 | `[(input_shape,), (input_shape,)]` 26 | beta_init: name of initialization function for shift parameter 27 | (see [initializations](../initializations.md)), or alternatively, 28 | Theano/TensorFlow function to use for weights initialization. 29 | This parameter is only relevant if you don't pass a `weights` argument. 30 | gamma_init: name of initialization function for scale parameter (see 31 | [initializations](../initializations.md)), or alternatively, 32 | Theano/TensorFlow function to use for weights initialization. 33 | This parameter is only relevant if you don't pass a `weights` argument. 34 | ''' 35 | def __init__(self, weights=None, axis=-1, momentum = 0.9, beta_init='zero', gamma_init='one', **kwargs): 36 | self.momentum = momentum 37 | self.axis = axis 38 | self.beta_init = initializations.get(beta_init) 39 | self.gamma_init = initializations.get(gamma_init) 40 | self.initial_weights = weights 41 | super(Scale, self).__init__(**kwargs) 42 | 43 | def build(self, input_shape): 44 | self.input_spec = [InputSpec(shape=input_shape)] 45 | shape = (int(input_shape[self.axis]),) 46 | 47 | # Tensorflow >= 1.0.0 compatibility 48 | self.gamma = K.variable(self.gamma_init(shape), name='{}_gamma'.format(self.name)) 49 | self.beta = K.variable(self.beta_init(shape), name='{}_beta'.format(self.name)) 50 | #self.gamma = self.gamma_init(shape, name='{}_gamma'.format(self.name)) 51 | #self.beta = self.beta_init(shape, name='{}_beta'.format(self.name)) 52 | self.trainable_weights = [self.gamma, self.beta] 53 | 54 | if self.initial_weights is not None: 55 | self.set_weights(self.initial_weights) 56 | del self.initial_weights 57 | 58 | def call(self, x, mask=None): 59 | input_shape = self.input_spec[0].shape 60 | broadcast_shape = [1] * len(input_shape) 61 | broadcast_shape[self.axis] = input_shape[self.axis] 62 | 63 | out = K.reshape(self.gamma, broadcast_shape) * x + K.reshape(self.beta, broadcast_shape) 64 | return out 65 | 66 | def get_config(self): 67 | config = {"momentum": self.momentum, "axis": self.axis} 68 | base_config = super(Scale, self).get_config() 69 | return dict(list(base_config.items()) + list(config.items())) -------------------------------------------------------------------------------- /TensorflowLearningNote/DenseNet_code_folder/mydensenet1.py: -------------------------------------------------------------------------------- 1 | ''' 2 | the script is denseNet Model for keras 3 | link: https://blog.csdn.net/shi2xian2wei2/article/details/84425777 4 | 5 | ''' 6 | import numpy as np 7 | import keras 8 | from keras.models import Model, save_model, load_model 9 | from keras.layers import Input, Dense, Dropout, BatchNormalization, LeakyReLU, concatenate 10 | from keras.layers import Conv2D, MaxPooling2D, AveragePooling2D, GlobalAveragePooling2D 11 | from keras.layers import Input, Activation 12 | 13 | 14 | def DenseLayer(x, nb_filter, bn_size=4, drop_rate=0.2): 15 | # Bottleneck layers 16 | x = BatchNormalization(axis=3)(x) 17 | x = Activation('relu')(x) 18 | x = Conv2D(bn_size*nb_filter, (1, 1), strides=(1, 1), padding='same')(x) 19 | 20 | # composite function 21 | x = BatchNormalization(axis=3)(x) 22 | x = Activation('relu')(x) 23 | x = Conv2D(nb_filter, (3, 3), strides=(1, 1), padding='same')(x) 24 | 25 | if drop_rate: 26 | x = Dropout(drop_rate)(x) 27 | 28 | return x 29 | 30 | 31 | def TransitionLayer(x, compression=0.5, is_max=0): 32 | nb_filter = int(x.shape.as_list()[-1]*compression) 33 | x = BatchNormalization(axis=3)(x) 34 | x = Activation('relu')(x) 35 | x = Conv2D(nb_filter, (1, 1), padding='same')(x) 36 | if is_max: 37 | x = MaxPooling2D(pool_size=(2, 2), strides=2)(x) 38 | else: 39 | x = AveragePooling2D(pool_size=(2, 2), strides=2)(x) 40 | 41 | return x 42 | 43 | 44 | def DenseBlock(x, nb_filter, growth_rate, drop_rate=0.2): 45 | for ii in range(nb_filter): 46 | conv = DenseLayer(x, nb_filter=growth_rate, drop_rate=drop_rate) 47 | x = concatenate([x, conv], axis=3) 48 | 49 | return x 50 | 51 | def DenseNet_model(input_shape, classes, growth_rate=12): 52 | inp = Input(shape=input_shape) 53 | x = Conv2D(growth_rate*2, (3, 3), strides=1, padding='same')(inp) 54 | x = BatchNormalization(axis=3)(x) 55 | x = Activation('relu')(x) 56 | 57 | x = DenseBlock(x, 12, growth_rate, drop_rate=0.2) 58 | x = TransitionLayer(x) 59 | 60 | x = DenseBlock(x, 12, growth_rate, drop_rate=0.2) 61 | x = TransitionLayer(x) 62 | 63 | x = DenseBlock(x, 12, growth_rate, drop_rate=0.2) 64 | x = BatchNormalization(axis=3)(x) 65 | x = GlobalAveragePooling2D()(x) 66 | 67 | x = Dense(classes, activation='softmax')(x) 68 | 69 | model = Model(inp, x) 70 | model.summary() 71 | 72 | return model 73 | 74 | if __name__ == '__main__': 75 | DenseNet_model(input_shape=(227, 227, 3), classes=1000) 76 | -------------------------------------------------------------------------------- /TensorflowLearningNote/LeNet-5_code_folder/mnist2image.py: -------------------------------------------------------------------------------- 1 | # _*_coding:utf-8_*_ 2 | import cv2 3 | import numpy as np 4 | import tensorflow as tf 5 | from tensorflow.examples.tutorials import mnist 6 | from tensorflow.examples.tutorials.mnist import input_data 7 | from PIL import Image 8 | import os 9 | 10 | 11 | # save raw image 12 | def save_raw(): 13 | # read data from mnist. if data not exist, will download automatically 14 | mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) 15 | # we save image raw data to data/raw/ folder 16 | # if the folder not there, create it 17 | save_dir = 'MNIST_data/raw/' 18 | if os.path.exists(save_dir) is False: 19 | os.makedirs(save_dir) 20 | 21 | # save 20 images in training dataset 22 | for i in range(20): 23 | # please attention,mnist.train.images[i, :] is ith image, sequence started from 0 24 | image_array = mnist.train.images[i, :] 25 | # the image in MNIST of TensorFlow, image is 784 length vector, we recover it to 28x28 image 26 | image_array = image_array.reshape(28, 28) 27 | # save image as mnist_train_0.jpg, mnist_train_1.jpg, ... ,mnist_train_19.jpg 28 | filename = save_dir + 'mnist_train_%d.jpg' % i 29 | # save image_array as image 30 | # use Image.fromarray to convert image,then call save function to save 31 | # because Image.fromarray is not good to support float, we have to convert it to uint8 and then read as 'L' 32 | Image.fromarray((image_array * 255).astype('uint8'), mode='L').convert('RGB').save(filename) 33 | 34 | 35 | def convert_mnist_img_raw(data, data1, save_path): 36 | for i in range(data.images.shape[0]): 37 | # TensorFlow中的MNIST图片是一个784维的向量,我们重新把它还原为28x28维的图像。 38 | image_array = data.images[i, :] 39 | image_array = image_array.reshape(28, 28) 40 | img_num = (image_array * 255).astype(np.uint8) 41 | # img_num = (image_array * 255).astype('uint8') 42 | label = data1.labels[i] 43 | # cv2.imshow('image', img) 44 | # cv2.waitKey(500) 45 | filename = save_path + '/{}_{}.jpg'.format(label, i) 46 | print(filename) 47 | cv2.imwrite(filename, img_num) 48 | # Image.fromarray(img_num, mode='L').save(filename) 49 | 50 | 51 | def convert_mnist_img(data, data1, save_path): 52 | for i in range(data.images.shape[0]): 53 | # TensorFlow中的MNIST图片是一个784维的向量,我们重新把它还原为28x28维的图像。 54 | image_array = data.images[i, :] 55 | image_array = image_array.reshape(28, 28) 56 | img_num = (image_array * 255).astype(np.uint8) 57 | # img_num = (image_array * 255).astype('uint8') 58 | label = data1.labels[i] 59 | if not os.path.exists(os.path.join(save_path, str(label))): 60 | os.mkdir(os.path.join(save_path, str(label))) 61 | # cv2.imshow('image', img) 62 | # cv2.waitKey(500) 63 | filename = save_path + '/' + str(label) + '/{}_{}.jpg'.format(label, i) 64 | print(filename) 65 | cv2.imwrite(filename, img_num) 66 | # Image.fromarray(img_num, mode='L').save(filename) 67 | 68 | 69 | 70 | 71 | if __name__ == '__main__': 72 | mnist = input_data.read_data_sets('MNIST_data/', one_hot=True) 73 | mnist1 = input_data.read_data_sets('MNIST_data/') 74 | content_name = ['MNIST_train', 'MNIST_test', 'MNIST_validation'] 75 | # print(mnist.validation.images.shape[0]) 76 | for i in content_name: 77 | if not os.path.exists(i): 78 | os.mkdir(i) 79 | # convert_mnist_img_raw(mnist.validation, mnist1.validation, 'MNIST_validation') # 55000 80 | convert_mnist_img(mnist.train, mnist1.train, 'MNIST_train') # 55000 81 | print('convert training data to image complete') 82 | convert_mnist_img(mnist.test, mnist1.test, 'MNIST_test') # 10000 83 | print('convert test data to image complete') 84 | convert_mnist_img(mnist.validation, mnist1.validation, 'MNIST_validation') # 5000 85 | print('convert validation data to image complete') 86 | -------------------------------------------------------------------------------- /TensorflowLearningNote/LeNet-5_code_folder/mnist2tfrecord.py: -------------------------------------------------------------------------------- 1 | #_*_coding:utf-8_*_ 2 | import tensorflow as tf 3 | from tensorflow.examples.tutorials.mnist import input_data 4 | import os 5 | import numpy as np 6 | 7 | # 生成整数的属性 8 | def _int64_feature(value): 9 | return tf.train.Feature(int64_list=tf.train.Int64List(value=[value])) 10 | 11 | # 生成字符串类型的属性 12 | def _bytes_feature(value): 13 | return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value])) 14 | 15 | def create_records(images, labels, num_example, outpout): 16 | ''' 17 | 实现将MNIST数据集转化为records 18 | 注意:读取的图像数据默认为uint8,然后转化为tf的字符串型BytesList保存 19 | :return: 20 | ''' 21 | # 训练图像的分辨率,作为example的属性 22 | pixels = images.shape[1] 23 | # 创建一个writer来写TFRecord文件 24 | writer = tf.python_io.TFRecordWriter(outpout) 25 | 26 | # 将每张图片都转化为一个Example 27 | for i in range(num_example): 28 | # 将图像转化为字符串 29 | image_raw = images[i].tostring() 30 | 31 | # 将一个样例转化为Example,Protocal Buffer并将所有信息写入这个数据结构 32 | example = tf.train.Example(features=tf.train.Features( 33 | feature={ 34 | 'pixels': _int64_feature(pixels), 35 | 'labels': _int64_feature(np.argmax(labels[i])), 36 | 'image_raw': _bytes_feature(image_raw) 37 | } 38 | )) 39 | 40 | # 将Example写入TFRecord文件 41 | writer.write(example.SerializeToString()) 42 | print("data processing success") 43 | writer.close() 44 | 45 | 46 | 47 | if __name__ == '__main__': 48 | if not os.path.exists('mnistrecord'): 49 | os.mkdir('mnistrecord') 50 | # 导入MNIST数据集 51 | mnist = input_data.read_data_sets('MNIST_data/', dtype=tf.uint8, one_hot=True) 52 | train_images = mnist.train.images 53 | train_labels = mnist.train.labels 54 | train_num_example = mnist.train.num_examples 55 | # 存储train_TFRecord文件的地址 56 | train_filename = 'mnistrecord/trainmnist28.tfrecords' 57 | create_records(train_images, train_labels, train_num_example, train_filename) 58 | 59 | test_images = mnist.test.images 60 | test_labels = mnist.test.labels 61 | test_num_example = mnist.test.num_examples 62 | # 存储train_TFRecord文件的地址 63 | test_filename = 'mnistrecord/testmnist28.tfrecords' 64 | create_records(test_images, test_labels, test_num_example, test_filename) 65 | -------------------------------------------------------------------------------- /TensorflowLearningNote/LeNet-5_code_folder/mylenet_trainminist.py: -------------------------------------------------------------------------------- 1 | # _*_coding:utf-8_*_ 2 | ''' 3 | LeNet是第一个成功应用于数字识别问题的卷积神经网络 4 | LeNet模型总共有7层,在MNIST上LeNet-5模型可以达到99.2%的正确率 5 | ''' 6 | import tensorflow as tf 7 | import tensorflow.examples.tutorials.mnist.input_data as input_data 8 | import time 9 | import os 10 | 11 | os.environ['CUDA_VISIBLE_DEVICES'] = '1,2' 12 | 13 | 14 | # 获取mnist数据 15 | mnist_data_set = input_data.read_data_sets('MNIST_data', one_hot=True) 16 | # 声明输入图片数据类型,mnist的手写体图片大小为28*28=784 17 | # None表示行向量的维度是任意的,也就是一次可以输出多张图片 18 | # placeholder 基本都是占位符,先定义,后面会用到的 19 | x = tf.placeholder('float', [None, 784]) 20 | # y_为网络的输出,数字十个类别 21 | y_ = tf.placeholder('float', [None, 10]) 22 | 23 | # 把输入的数据转变成二维形式,用于卷积计算 24 | # -1表示一次可以存储多张照片,1表示图像的通道数为1 25 | x_image = tf.reshape(x, [-1, 28, 28, 1]) 26 | 27 | # 卷积核初始化,大小为6个5*5的卷积核,1和x_image的1对应,即为图像的通道数 28 | filter1 = tf.Variable(tf.truncated_normal([5, 5, 1, 6])) 29 | # 偏置项 30 | bias1 = tf.Variable(tf.truncated_normal([6])) 31 | # 二维卷积计算 32 | conv1 = tf.nn.conv2d(x_image, filter1, strides=[1, 1, 1, 1], padding='SAME') 33 | h_conv1 = tf.nn.sigmoid(conv1 + bias1) 34 | # 池化层 35 | maxPool2 = tf.nn.max_pool(h_conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') 36 | 37 | filter2 = tf.Variable(tf.truncated_normal([5, 5, 6, 16])) 38 | bias2 = tf.Variable(tf.truncated_normal([16])) 39 | conv2 = tf.nn.conv2d(maxPool2, filter2, strides=[1, 1, 1, 1], padding='SAME') 40 | h_conv2 = tf.nn.sigmoid(conv2 + bias2) 41 | 42 | maxPool3 = tf.nn.max_pool(h_conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') 43 | 44 | filter3 = tf.Variable(tf.truncated_normal([5, 5, 16, 120])) 45 | bias3 = tf.Variable(tf.truncated_normal([120])) 46 | conv3 = tf.nn.conv2d(maxPool3, filter3, strides=[1, 1, 1, 1], padding='SAME') 47 | h_conv3 = tf.nn.sigmoid(conv3 + bias3) 48 | 49 | # 全连接层,权重初始化 50 | W_fc1 = tf.Variable(tf.truncated_normal([7 * 7 * 120, 80])) 51 | # 偏置项 52 | b_fc1 = tf.Variable(tf.truncated_normal([80])) 53 | # 将卷积的输出展开 54 | h_pool2_flat = tf.reshape(h_conv3, [-1, 7 * 7 * 120]) 55 | h_fc1 = tf.nn.sigmoid(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) 56 | 57 | W_fc2 = tf.Variable(tf.truncated_normal([80, 10])) 58 | b_fc2 = tf.Variable(tf.truncated_normal([10])) 59 | # 输出层,使用softmax进行多分类 60 | y_conv = tf.nn.softmax(tf.matmul(h_fc1, W_fc2) + b_fc2) 61 | 62 | # 损失函数,交叉熵 63 | cross_entropy = -tf.reduce_sum(y_ * tf.log(y_conv)) 64 | # 使用梯度下降法来更新权重,学习速率为0.001,改为0.01的话会导致权重更新有问题,准确率会滴 65 | train_step = tf.train.GradientDescentOptimizer(0.001).minimize(cross_entropy) 66 | 67 | sess = tf.InteractiveSession() 68 | 69 | # 测试准确率,tf.argmax() 计算行或列的最大值,返回最大值下标的向量 70 | # tf.equal() 计算两个向量对应的元素是否相等,返回数据为bool 71 | # tf.cast() 把bool数据转化为浮点型 72 | correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1)) 73 | accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float')) 74 | 75 | # 对所有变量进行初始化 76 | sess.run(tf.global_variables_initializer()) 77 | 78 | start_time = time.time() 79 | for i in range(50000): 80 | # 获取训练数据,每次100张 81 | # 我们后面取数据的时候,直接获取feed_dict={x: batch[0], y_true: batch[1]} 82 | # batch = mnist_data_set.train.next_batch(60) 83 | batch_xs, batch_ys = mnist_data_set.train.next_batch(100) 84 | train_step.run(feed_dict={x: batch_xs, y_: batch_ys}) 85 | # 每个100次输出当前的准确率 86 | if i % 100 == 0: 87 | train_accuracy = accuracy.eval(feed_dict={x: batch_xs, y_: batch_ys}) 88 | print('step %d, training accuracy %g' % (i, train_accuracy)) 89 | # 计算时间间隔 90 | end_time = time.time() 91 | print('time: ', (end_time - start_time)) 92 | start_time = end_time 93 | 94 | print("Test accuracy: {}".format(accuracy.eval(session=sess, 95 | feed_dict={ 96 | x: mnist_data_set.test.images, 97 | y_: mnist_data_set.test.labels}))) 98 | 99 | # 关闭会话 100 | sess.close() 101 | -------------------------------------------------------------------------------- /TensorflowLearningNote/LeNet-5_code_folder/parsedata.py: -------------------------------------------------------------------------------- 1 | # _*_coding:utf-8_*_ 2 | import tensorflow as tf 3 | import os 4 | 5 | # os.environ['CUDA_VISIBLE_DEVICES'] = '1' 6 | 7 | # 1,输入数据的解析和预处理 8 | def read_records(filename, resize_height, resize_width, type=None): 9 | ''' 10 | 解析record文件:源文件的图像数据是RGB,uint8 【0, 255】一般作为训练数据时,需要归一化到[0, 1] 11 | :param filename: 12 | :param resize_height: 13 | :param resize_width: 14 | :param type: 选择图像数据的返回类型 15 | None:默认将uint8-[0,255]转为float32-[0,255] 16 | normalization:归一化float32-[0,1] 17 | centralization:归一化float32-[0,1],再减均值中心化 18 | :return: 19 | ''' 20 | # 创建文件队列,不限读取的数量 21 | filename_queue = tf.train.string_input_producer([filename]) 22 | # create a reader from file queue 23 | reader = tf.TFRecordReader() 24 | # reader 从文件队列中读入一个序列化的样本 25 | _, serialized_example = reader.read(filename_queue) 26 | # get feature from serialized example 27 | # 解析符号化的样本 28 | features = tf.parse_single_example( 29 | serialized_example, 30 | features={ 31 | 'image_raw': tf.FixedLenFeature([], tf.string), 32 | 'height': tf.FixedLenFeature([], tf.int64), 33 | 'width': tf.FixedLenFeature([], tf.int64), 34 | 'depth': tf.FixedLenFeature([], tf.int64), 35 | 'label': tf.FixedLenFeature([], tf.int64) 36 | } 37 | ) 38 | tf_image = tf.decode_raw(features['image_raw'], tf.uint8) # 获得图像原始的数据 39 | 40 | tf_height = features['height'] 41 | tf_width = features['width'] 42 | tf_depth = features['depth'] 43 | tf_label = tf.cast(features['label'], tf.int32) 44 | # PS: 恢复原始图像数据,reshape的大小必须与保存之前的图像shape一致,否则出错 45 | tf_image = tf.reshape(tf_image, [resize_height, resize_width, 3]) # 设置图像的维度 46 | 47 | # 存储的图像类型为uint8,TensorFlow训练时数据必须是tf.float32 48 | if type is None: 49 | tf_image = tf.cast(tf_image, tf.float32) 50 | elif type == 'normalization': # [1]若需要归一化请使用: 51 | # 仅当输入数据是uint8,才会归一化[0,255] 52 | # tf_image = tf.image.convert_image_dtype(tf_image, tf.float32) 53 | tf_image = tf.cast(tf_image, tf.float32) * (1. / 255.0) # 归一化 54 | elif type == 'centralization': 55 | # 若需要归一化,且中心化,假设均值为0.5,请使用: 56 | tf_image = tf.cast(tf_image, tf.float32) * (1. / 255) - 0.5 # 中心化 57 | 58 | # 这里仅仅返回图像和标签 59 | # return tf_image, tf_height,tf_width,tf_depth,tf_label 60 | return tf_image, tf_label 61 | 62 | 63 | def get_batch_images(images, labels, batch_size, labels_nums, one_hot=False, 64 | shuffle=False, num_threads=1): 65 | ''' 66 | :param images: 图像 67 | :param labels: 标签 68 | :param batch_size: 69 | :param labels_nums: 标签个数 70 | :param one_hot: 是否将labels转化为one_hot 的形式 71 | :param shuffle: 是否打乱顺序,一般train时,shuffle=True,验证时shuffle=False 72 | :param num_threads: 73 | :return: 返回batch的images和labels 74 | ''' 75 | min_after_dequeue = 200 76 | # 保证 capacity必须大于 min_after_dequeue的参数值 77 | capacity = min_after_dequeue + 3 * batch_size 78 | if shuffle: 79 | images_batch, labels_batch = tf.train.shuffle_batch([images, labels], 80 | batch_size=batch_size, 81 | capacity=capacity, 82 | min_after_dequeue=min_after_dequeue, 83 | num_threads=num_threads) 84 | else: 85 | images_batch, labels_batch = tf.train.batch([images, labels], 86 | batch_size=batch_size, 87 | capacity=capacity, 88 | num_threads=num_threads) 89 | if one_hot: 90 | labels_batch = tf.one_hot(labels_batch, labels_nums, 1, 0) 91 | return images_batch, labels_batch 92 | -------------------------------------------------------------------------------- /TensorflowLearningNote/LeNet-5_code_folder/readtfrecord.py: -------------------------------------------------------------------------------- 1 | # _*_coding:utf-8_*_ 2 | import tensorflow as tf 3 | 4 | 5 | def read_record(filename): 6 | ''' 7 | 读取TFRecord文件 8 | :return: 9 | ''' 10 | # 创建一个队列来维护输入文件列表 11 | filename_queue = tf.train.string_input_producer([filename]) 12 | # 创建一个reader来读取TFRecord文件中Example 13 | reader = tf.TFRecordReader() 14 | # 从文件中读取一个Example 15 | _, serialized_example = reader.read(filename_queue) 16 | 17 | # 用FixedLenFeature 将读入的Example解析成tensor 18 | features = tf.parse_single_example( 19 | serialized_example, 20 | features={ 21 | 'image_raw': tf.FixedLenFeature([], tf.string), 22 | 'pixels': tf.FixedLenFeature([], tf.int64), 23 | 'labels': tf.FixedLenFeature([], tf.int64) 24 | } 25 | ) 26 | 27 | # tf.decode_raw将字符串解析成图像对应的像素数组 28 | images = tf.decode_raw(features['image_raw'], tf.uint8) 29 | labels = tf.cast(features['labels'], tf.int32) 30 | pixels = tf.cast(features['pixels'], tf.int32) 31 | 32 | init_op = tf.global_variables_initializer() 33 | 34 | with tf.Session() as sess: 35 | sess.run(init_op) 36 | # 启动多线程处理输入数据 37 | coord = tf.train.Coordinator() 38 | threads = tf.train.start_queue_runners(sess=sess, coord=coord) 39 | 40 | # 每次运行读出一个Example,当所有样例读取完之后,再次样例中程序会重头 读取 41 | for i in range(10): 42 | # 在会话中会取出image 和label 43 | image, label = sess.run([images, labels]) 44 | coord.request_stop() 45 | coord.join(threads) 46 | print("end code") 47 | 48 | 49 | if __name__ == '__main__': 50 | train_filename = 'mnistrecord/trainmnist28.tfrecords' 51 | test_filename = 'mnistrecord/testmnist28.tfrecords' 52 | read_record(filename=train_filename) 53 | -------------------------------------------------------------------------------- /TensorflowLearningNote/MobileNet_code_folder/mymobilenetv2.py: -------------------------------------------------------------------------------- 1 | from keras.models import Model 2 | from keras.layers import Input, Conv2D, GlobalAveragePooling2D, Dropout 3 | from keras.layers import Activation, BatchNormalization, add, Reshape 4 | from keras.applications.mobilenet import relu6, DepthwiseConv2D 5 | from keras.utils.vis_utils import plot_model 6 | 7 | from keras import backend as K 8 | 9 | 10 | def _conv_block(inputs, filters, kernel, strides): 11 | """Convolution Block 12 | This function defines a 2D convolution operation with BN and relu6. 13 | # Arguments 14 | inputs: Tensor, input tensor of conv layer. 15 | filters: Integer, the dimensionality of the output space. 16 | kernel: An integer or tuple/list of 2 integers, specifying the 17 | width and height of the 2D convolution window. 18 | strides: An integer or tuple/list of 2 integers, 19 | specifying the strides of the convolution along the width and height. 20 | Can be a single integer to specify the same value for 21 | all spatial dimensions. 22 | # Returns 23 | Output tensor. 24 | """ 25 | 26 | channel_axis = 1 if K.image_data_format() == 'channels_first' else -1 27 | 28 | x = Conv2D(filters, kernel, padding='same', strides=strides)(inputs) 29 | x = BatchNormalization(axis=channel_axis)(x) 30 | return Activation(relu6)(x) 31 | 32 | 33 | def _bottleneck(inputs, filters, kernel, t, s, r=False): 34 | """Bottleneck 35 | This function defines a basic bottleneck structure. 36 | # Arguments 37 | inputs: Tensor, input tensor of conv layer. 38 | filters: Integer, the dimensionality of the output space. 39 | kernel: An integer or tuple/list of 2 integers, specifying the 40 | width and height of the 2D convolution window. 41 | t: Integer, expansion factor. 42 | t is always applied to the input size. 43 | s: An integer or tuple/list of 2 integers,specifying the strides 44 | of the convolution along the width and height.Can be a single 45 | integer to specify the same value for all spatial dimensions. 46 | r: Boolean, Whether to use the residuals. 47 | # Returns 48 | Output tensor. 49 | """ 50 | 51 | channel_axis = 1 if K.image_data_format() == 'channels_first' else -1 52 | tchannel = K.int_shape(inputs)[channel_axis] * t 53 | 54 | x = _conv_block(inputs, tchannel, (1, 1), (1, 1)) 55 | 56 | x = DepthwiseConv2D(kernel, strides=(s, s), depth_multiplier=1, padding='same')(x) 57 | x = BatchNormalization(axis=channel_axis)(x) 58 | x = Activation(relu6)(x) 59 | 60 | x = Conv2D(filters, (1, 1), strides=(1, 1), padding='same')(x) 61 | x = BatchNormalization(axis=channel_axis)(x) 62 | 63 | if r: 64 | x = add([x, inputs]) 65 | return x 66 | 67 | 68 | def _inverted_residual_block(inputs, filters, kernel, t, strides, n): 69 | """Inverted Residual Block 70 | This function defines a sequence of 1 or more identical layers. 71 | # Arguments 72 | inputs: Tensor, input tensor of conv layer. 73 | filters: Integer, the dimensionality of the output space. 74 | kernel: An integer or tuple/list of 2 integers, specifying the 75 | width and height of the 2D convolution window. 76 | t: Integer, expansion factor. 77 | t is always applied to the input size. 78 | s: An integer or tuple/list of 2 integers,specifying the strides 79 | of the convolution along the width and height.Can be a single 80 | integer to specify the same value for all spatial dimensions. 81 | n: Integer, layer repeat times. 82 | # Returns 83 | Output tensor. 84 | """ 85 | 86 | x = _bottleneck(inputs, filters, kernel, t, strides) 87 | 88 | for i in range(1, n): 89 | x = _bottleneck(x, filters, kernel, t, 1, True) 90 | 91 | return x 92 | 93 | 94 | def MobileNetv2(input_shape, k): 95 | """MobileNetv2 96 | This function defines a MobileNetv2 architectures. 97 | # Arguments 98 | input_shape: An integer or tuple/list of 3 integers, shape 99 | of input tensor. 100 | k: Integer, layer repeat times. 101 | # Returns 102 | MobileNetv2 model. 103 | """ 104 | 105 | inputs = Input(shape=input_shape) 106 | x = _conv_block(inputs, 32, (3, 3), strides=(2, 2)) 107 | 108 | x = _inverted_residual_block(x, 16, (3, 3), t=1, strides=1, n=1) 109 | x = _inverted_residual_block(x, 24, (3, 3), t=6, strides=2, n=2) 110 | x = _inverted_residual_block(x, 32, (3, 3), t=6, strides=2, n=3) 111 | x = _inverted_residual_block(x, 64, (3, 3), t=6, strides=2, n=4) 112 | x = _inverted_residual_block(x, 96, (3, 3), t=6, strides=1, n=3) 113 | x = _inverted_residual_block(x, 160, (3, 3), t=6, strides=2, n=3) 114 | x = _inverted_residual_block(x, 320, (3, 3), t=6, strides=1, n=1) 115 | 116 | x = _conv_block(x, 1280, (1, 1), strides=(1, 1)) 117 | x = GlobalAveragePooling2D()(x) 118 | x = Reshape((1, 1, 1280))(x) 119 | x = Dropout(0.3, name='Dropout')(x) 120 | x = Conv2D(k, (1, 1), padding='same')(x) 121 | 122 | x = Activation('softmax', name='softmax')(x) 123 | output = Reshape((k,))(x) 124 | 125 | model = Model(inputs, output) 126 | plot_model(model, to_file='images/MobileNetv2.png', show_shapes=True) 127 | 128 | return model 129 | 130 | 131 | if __name__ == '__main__': 132 | MobileNetv2((224, 224, 3), 1000) 133 | -------------------------------------------------------------------------------- /TensorflowLearningNote/README.md: -------------------------------------------------------------------------------- 1 | # Tensorflow学习笔记地址 2 | *** 3 | ###### 摘要 4 | > 这个文件夹存放的是这里存放自己学习Tensorflow的相关代码,和学习Tensorflow的博客地址,希望可以帮助到别人 5 | *** 6 | *** 7 | ### 卷积神经网络学习笔记——轻量化网络MobileNet系列(V1,V2,V3) 8 | - 地址:https://www.cnblogs.com/wj-1314/p/10494911.html 9 | #### 关于此博客的代码与数据在: MobileNet_code_folder 10 | *** 11 | *** 12 | ### 卷积神经网络学习笔记——SENet 13 | - 地址:https://www.cnblogs.com/wj-1314/p/14147932.html 14 | #### 关于此博客的代码与数据在: SENet_code_folder 15 | *** 16 | *** 17 | ### 卷积神经网络学习笔记——DenseNet 18 | - 地址:https://www.cnblogs.com/wj-1314/p/12706423.html 19 | #### 关于此博客的代码与数据在: DenseNet_code_folder 20 | *** 21 | *** 22 | ### tensorflow学习笔记——ResNet 23 | - 地址:https://www.cnblogs.com/wj-1314/p/11519663.html 24 | #### 关于此博客的代码与数据在: ResNet_code_folder 25 | *** 26 | ### tensorflow学习笔记——GoogLeNet 27 | - 地址:https://www.cnblogs.com/wj-1314/p/11337807.html 28 | #### 关于此博客的代码与数据在: GoogLeNet_code_folder 29 | *** 30 | *** 31 | ### tensorflow学习笔记——VGGNet 32 | - 地址:https://www.cnblogs.com/wj-1314/p/11502563.html 33 | #### 关于此博客的代码与数据在: VGGNet_code_folder 34 | *** 35 | *** 36 | ### tensorflow学习笔记——AlexNet 37 | - 地址:https://www.cnblogs.com/wj-1314/p/10767561.html 38 | #### 关于此博客的代码与数据在: AlexNet_code_folder 39 | *** 40 | *** 41 | ### tensorflow学习笔记——LeNet-5(训练自己的数据集) 42 | - 地址:https://www.cnblogs.com/wj-1314/p/11858502.html 43 | #### 关于此博客的代码与数据在: LeNet-5_code_folder 44 | *** 45 | *** 46 | -------------------------------------------------------------------------------- /TensorflowLearningNote/ResNet_code_folder/resnet50_2.py: -------------------------------------------------------------------------------- 1 | #_*_coding:utf-8_*_ 2 | from keras.models import Model 3 | from keras.layers import Input, Dense, BatchNormalization, Conv2D, MaxPooling2D, Convolution2D 4 | from keras.layers import AveragePooling2D, ZeroPadding2D 5 | from keras.layers import add, Flatten, Activation, concatenate 6 | from keras.optimizers import SGD 7 | import numpy as np 8 | 9 | 10 | def identity_block(input_tensor, nb_filter, kernel_size=3): 11 | ''' 12 | 直接相加,并不需要 1*1 卷积 13 | input_tensor:输入 14 | nb_filter:卷积核个数,需要按顺序指定3个,例如(64, 64, 256) 15 | kernel_size:卷积核大小 16 | ''' 17 | nb_filter1, nb_filter2, nb_filter3 = nb_filter 18 | 19 | out = Conv2D(nb_filter1, kernel_size=(1, 1), strides=(1, 1), padding='valid')(input_tensor) 20 | out = BatchNormalization()(out) 21 | out = Activation('relu')(out) 22 | 23 | out = Conv2D(nb_filter2, kernel_size=(kernel_size, kernel_size), strides=(1, 1), padding='same')(out) 24 | out = BatchNormalization()(out) 25 | out = Activation('relu')(out) 26 | 27 | out = Conv2D(nb_filter3, kernel_size=(1, 1), strides=(1, 1), padding='valid')(out) 28 | out = BatchNormalization()(out) 29 | 30 | out = add([out, input_tensor]) 31 | out = Activation('relu')(out) 32 | 33 | return out 34 | 35 | 36 | def conv_block(input_tensor, nb_filter, kernel_size=3): 37 | nb_filter1, nb_filter2, nb_filter3 = nb_filter 38 | 39 | # 有人这里 strides=(2,2) 40 | out = Conv2D(nb_filter1, kernel_size=(1, 1), strides=(1, 1), padding='valid')(input_tensor) 41 | out = BatchNormalization()(out) 42 | out = Activation('relu')(out) 43 | 44 | out = Conv2D(nb_filter2, kernel_size=(kernel_size, kernel_size), strides=(1, 1), padding='same')(out) 45 | out = BatchNormalization()(out) 46 | out = Activation('relu')(out) 47 | 48 | out = Conv2D(nb_filter3, kernel_size=(1, 1), strides=(1, 1), padding='valid')(out) 49 | out = BatchNormalization()(out) 50 | 51 | shortcut = Conv2D(nb_filter3, kernel_size=(1, 1), strides=(1, 1), padding='valid')(input_tensor) 52 | shortcut = BatchNormalization()(shortcut) 53 | 54 | out = add([out, shortcut]) 55 | out = Activation('relu')(out) 56 | 57 | return out 58 | 59 | 60 | def resnet50(input_shape, classes): 61 | ''' 62 | input_shape = (224, 224, 3) 63 | ''' 64 | # define the input as a tensor with shape input_shape 65 | X_input = Input(shape=input_shape) 66 | 67 | X = ZeroPadding2D((3, 3))(X_input) 68 | 69 | # stage 1 70 | X = Conv2D(64, kernel_size=(7, 7), strides=(2, 2))(X) 71 | X = BatchNormalization()(X) 72 | X = Activation('relu')(X) 73 | X = MaxPooling2D(pool_size=(3, 3), strides=(2, 2))(X) 74 | 75 | # stage 2 76 | X = conv_block(X, [64, 64, 256]) 77 | X = identity_block(X, [64, 64, 256]) 78 | X = identity_block(X, [64, 64, 256]) 79 | 80 | # stage 3 81 | X = conv_block(X, [128, 128, 512]) 82 | X = identity_block(X, [128, 128, 512]) 83 | X = identity_block(X, [128, 128, 512]) 84 | X = identity_block(X, [128, 128, 512]) 85 | 86 | 87 | # stage 4 88 | X = conv_block(X, [256, 256, 1024]) 89 | X = identity_block(X, [256, 256, 1024]) 90 | X = identity_block(X, [256, 256, 1024]) 91 | X = identity_block(X, [256, 256, 1024]) 92 | X = identity_block(X, [256, 256, 1024]) 93 | X = identity_block(X, [256, 256, 1024]) 94 | 95 | # stage 5 96 | X = conv_block(X, [512, 512, 2048]) 97 | X = identity_block(X, [512, 512, 2048]) 98 | X = identity_block(X, [512, 512, 2048]) 99 | 100 | X = AveragePooling2D((7, 7))(X) 101 | # X = AveragePooling2D(pool_size=(2, 2), padding="same")(X) 102 | X = Flatten()(X) 103 | X_out = Dense(classes, activation='softmax')(X) 104 | 105 | model = Model(X_input, X_out) 106 | 107 | model.summary() 108 | 109 | return model 110 | 111 | 112 | resnet50(input_shape=(224, 224, 3), classes=1000) -------------------------------------------------------------------------------- /TensorflowLearningNote/SENet_code_folder/SE_Inception.py: -------------------------------------------------------------------------------- 1 | def build_SE_model(nb_classes, input_shape=(256, 256, 3)): 2 | inputs_dim = Input(input_shape) 3 | x = Inception(include_top=False, weights='imagenet', input_shape=None, 4 | pooling=max)(inputs_dim) 5 | 6 | squeeze = GlobalAveragePooling2D()(x) 7 | 8 | excitation = Dense(units=2048//16)(squeeze) 9 | excitation = Activation('relu')(excitation) 10 | excitation = Dense(units=2048)(excitation) 11 | excitation = Activation('sigmoid')(excitation) 12 | excitation = Reshape((1, 1, 2048))(excitation) 13 | 14 | scale = multiply([x, excitation]) 15 | 16 | x = GlobalAveragePooling2D()(scale) 17 | dp_1 = Dropout(0.3)(x) 18 | fc2 = Dense(nb_classes)(dp_1) 19 | # 此处注意,为Sigmoid函数 20 | fc2 = Activation('sigmoid')(fc2) 21 | model = Model(inputs=inputs_dim, outputs=fc2) 22 | return model 23 | 24 | 25 | if __name__ == '__main__': 26 | model =build_model(nb_classes, input_shape=(im_size1, im_size2, channels)) 27 | opt = Adam(lr=2*1e-5) 28 | model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy']) 29 | model.fit() 30 | -------------------------------------------------------------------------------- /TensorflowLearningNote/SENet_code_folder/train_cifar10.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from scipy.misc import imresize 3 | from keras.utils import np_utils 4 | from keras.datasets import cifar10 5 | from keras.preprocessing.image import ImageDataGenerator 6 | from keras import optimizers 7 | from keras.callbacks import LearningRateScheduler, CSVLogger, ModelCheckpoint 8 | import keras.backend as K 9 | from keras.backend import tensorflow_backend 10 | import tensorflow as tf 11 | 12 | from model.SEResNeXt import SEResNeXt 13 | 14 | import json 15 | import configparser 16 | 17 | 18 | 19 | def arr_resize(arr, size): 20 | ''' 21 | Resize Colored image array. 22 | ''' 23 | resized_arr = np.empty((arr.shape[0],size,size,3)) 24 | for idx, elem in enumerate(arr): 25 | resized_arr[idx] = imresize(elem, (size,size,3), interp='bilinear') 26 | 27 | return resized_arr 28 | 29 | 30 | ## Load parameters 31 | inifile = configparser.ConfigParser() 32 | inifile.read("./config.ini") 33 | size = int(inifile.get('cifar10','size')) 34 | num_classes = int(inifile.get('cifar10','num_classes')) 35 | batch_size = int(inifile.get('cifar10','batch_size')) 36 | 37 | ## Memory setting 38 | config = tf.ConfigProto(gpu_options=tf.GPUOptions(allow_growth=True)) 39 | session = tf.Session(config=config) 40 | K.set_session(session) 41 | 42 | ## Data preparation 43 | (x_train, y_train), (x_test, y_test) = cifar10.load_data() 44 | y_train = np_utils.to_categorical(y_train, num_classes) 45 | y_test = np_utils.to_categorical(y_test, num_classes) 46 | 47 | x_train = arr_resize(x_train, size) 48 | x_test = arr_resize(x_test, size) 49 | 50 | datagen = ImageDataGenerator( 51 | rescale = 1/255. 52 | , shear_range = 0.1 53 | , zoom_range = 0.1 54 | , channel_shift_range=0.1 55 | , rotation_range=15 56 | , width_shift_range=0.2 57 | , height_shift_range=0.2 58 | , horizontal_flip=True) 59 | datagen.fit(x_train) 60 | 61 | valid_datagen = ImageDataGenerator(rescale = 1/255.) 62 | valid_datagen.fit(x_test) 63 | 64 | 65 | ## Create and compile a model 66 | model = SEResNeXt(size, num_classes).model 67 | sgd = optimizers.SGD(lr=0.1, momentum=0.9, nesterov=True) 68 | 69 | def lr_scheduler(epoch): 70 | if epoch % 30 == 0: 71 | K.set_value(model.optimizer.lr, K.eval(model.optimizer.lr) * 0.1) 72 | return K.eval(model.optimizer.lr) 73 | change_lr = LearningRateScheduler(lr_scheduler) 74 | 75 | model.compile( 76 | optimizer=sgd 77 | , loss='categorical_crossentropy' 78 | , metrics=['accuracy']) 79 | 80 | 81 | ## Set callbacks 82 | model_save_name = "./trained_model/SEResNeXt" 83 | filepath = model_save_name + "-{epoch:02d}-{val_acc:.3f}.h5" 84 | 85 | csv_logger = CSVLogger('./logs/training.log') 86 | checkpoint = ModelCheckpoint( 87 | filepath 88 | , monitor='val_acc' 89 | , verbose=5 90 | , save_best_only=True 91 | , mode='max' 92 | ) 93 | 94 | 95 | ## Model training 96 | with open("{0}.json".format(model_save_name), 'w') as f: 97 | json.dump(json.loads(model.to_json()), f) # model.to_json() is a STRING of json 98 | 99 | model.fit_generator( 100 | datagen.flow(x_train, y_train, batch_size=batch_size) 101 | , steps_per_epoch=len(x_train) // batch_size 102 | , epochs=100 103 | , validation_data = valid_datagen.flow(x_test, y_test) 104 | , validation_steps=len(x_test) // batch_size 105 | , callbacks=[change_lr, csv_logger, checkpoint]) 106 | 107 | model.save_weights('{0}.h5'.format(model_save_name)) 108 | -------------------------------------------------------------------------------- /TensorflowLearningNote/SiameseNet_code_folder/keras_siamese_mnist.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | from __future__ import print_function 3 | import numpy as np 4 | 5 | import random 6 | from keras.datasets import mnist 7 | from keras.models import Model 8 | from keras.layers import Input, Flatten, Dense, Dropout, Lambda 9 | from keras.optimizers import RMSprop 10 | from keras import backend as K 11 | 12 | num_classes = 10 13 | epochs = 20 14 | 15 | 16 | def euclidean_distance(vects): 17 | x, y = vects 18 | sum_square = K.sum(K.square(x - y), axis=1, keepdims=True) 19 | return K.sqrt(K.maximum(sum_square, K.epsilon())) 20 | 21 | 22 | def eucl_dist_output_shape(shapes): 23 | shape1, shape2 = shapes 24 | return (shape1[0], 1) 25 | 26 | 27 | def contrastive_loss(y_true, y_pred): 28 | '''Contrastive loss from Hadsell-et-al.'06 29 | http://yann.lecun.com/exdb/publis/pdf/hadsell-chopra-lecun-06.pdf 30 | ''' 31 | margin = 1 32 | square_pred = K.square(y_pred) 33 | margin_square = K.square(K.maximum(margin - y_pred, 0)) 34 | return K.mean(y_true * square_pred + (1 - y_true) * margin_square) 35 | 36 | 37 | def create_pairs(x, digit_indices): 38 | '''Positive and negative pair creation. 39 | Alternates between positive and negative pairs. 40 | ''' 41 | pairs = [] 42 | labels = [] 43 | n = min([len(digit_indices[d]) for d in range(num_classes)]) - 1 44 | for d in range(num_classes): 45 | for i in range(n): 46 | z1, z2 = digit_indices[d][i], digit_indices[d][i + 1] 47 | pairs += [[x[z1], x[z2]]] 48 | inc = random.randrange(1, num_classes) 49 | dn = (d + inc) % num_classes 50 | z1, z2 = digit_indices[d][i], digit_indices[dn][i] 51 | pairs += [[x[z1], x[z2]]] 52 | labels += [1, 0] 53 | return np.array(pairs), np.array(labels) 54 | 55 | 56 | def create_base_network(input_shape): 57 | '''Base network to be shared (eq. to feature extraction). 58 | ''' 59 | input = Input(shape=input_shape) 60 | x = Flatten()(input) 61 | x = Dense(128, activation='relu')(x) 62 | x = Dropout(0.1)(x) 63 | x = Dense(128, activation='relu')(x) 64 | x = Dropout(0.1)(x) 65 | x = Dense(128, activation='relu')(x) 66 | return Model(input, x) 67 | 68 | 69 | def compute_accuracy(y_true, y_pred): 70 | '''Compute classification accuracy with a fixed threshold on distances. 71 | ''' 72 | pred = y_pred.ravel() < 0.5 73 | return np.mean(pred == y_true) 74 | 75 | 76 | def accuracy(y_true, y_pred): 77 | '''Compute classification accuracy with a fixed threshold on distances. 78 | ''' 79 | return K.mean(K.equal(y_true, K.cast(y_pred < 0.5, y_true.dtype))) 80 | 81 | 82 | # the data, split between train and test sets 83 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 84 | x_train = x_train.astype('float32') 85 | x_test = x_test.astype('float32') 86 | x_train /= 255 87 | x_test /= 255 88 | input_shape = x_train.shape[1:] 89 | 90 | # create training+test positive and negative pairs 91 | digit_indices = [np.where(y_train == i)[0] for i in range(num_classes)] 92 | tr_pairs, tr_y = create_pairs(x_train, digit_indices) 93 | 94 | digit_indices = [np.where(y_test == i)[0] for i in range(num_classes)] 95 | te_pairs, te_y = create_pairs(x_test, digit_indices) 96 | 97 | # network definition 98 | base_network = create_base_network(input_shape) 99 | 100 | input_a = Input(shape=input_shape) 101 | input_b = Input(shape=input_shape) 102 | 103 | # because we re-use the same instance `base_network`, 104 | # the weights of the network 105 | # will be shared across the two branches 106 | processed_a = base_network(input_a) 107 | processed_b = base_network(input_b) 108 | 109 | distance = Lambda(euclidean_distance, 110 | output_shape=eucl_dist_output_shape)([processed_a, processed_b]) 111 | 112 | model = Model([input_a, input_b], distance) 113 | 114 | # train 115 | rms = RMSprop() 116 | model.compile(loss=contrastive_loss, optimizer=rms, metrics=[accuracy]) 117 | model.fit([tr_pairs[:, 0], tr_pairs[:, 1]], tr_y, 118 | batch_size=128, 119 | epochs=epochs, 120 | validation_data=([te_pairs[:, 0], te_pairs[:, 1]], te_y)) 121 | 122 | # compute final accuracy on training and test sets 123 | y_pred = model.predict([tr_pairs[:, 0], tr_pairs[:, 1]]) 124 | tr_acc = compute_accuracy(tr_y, y_pred) 125 | y_pred = model.predict([te_pairs[:, 0], te_pairs[:, 1]]) 126 | te_acc = compute_accuracy(te_y, y_pred) 127 | 128 | print('* Accuracy on training set: %0.2f%%' % (100 * tr_acc)) 129 | print('* Accuracy on test set: %0.2f%%' % (100 * te_acc)) 130 | -------------------------------------------------------------------------------- /TensorflowLearningNote/SiameseNet_code_folder/keras_siamese_predict.py: -------------------------------------------------------------------------------- 1 | # load data 2 | def get_image_data(imagePaths, label): 3 | data = [] 4 | labels = [] 5 | for image_name in os.listdir(imagePaths): 6 | imagePath = os.path.join(imagePaths, image_name) 7 | image = cv2.imread(imagePath) 8 | image = cv2.resize(image, target_size) 9 | data.append(image) 10 | labels.append(label) 11 | data = np.array(data, dtype='float') 12 | data /= 255.0 13 | labels = np.array(labels) 14 | data, labels = shuffle(data, labels, random_state=0) 15 | return data, labels 16 | 17 | 18 | def load_train_test_data(): 19 | filelist = [] 20 | for i in os.listdir(file_path): 21 | filelist.append(i) 22 | data = [] 23 | labels = [] 24 | for i in range(len(filelist)): 25 | filedir = filelist[i] 26 | allpath = os.path.join(file_path, filelist[i]) 27 | data_i, labels_i = get_image_data(imagePaths=allpath, label=filedir) 28 | data_i, labels_i = list(data_i), list(labels_i) 29 | data.extend(data_i) 30 | labels.extend(labels_i) 31 | data, labels = np.array(data), np.array(labels) 32 | x_train, x_test, y_train, y_test = train_test_split(data, labels, test_size=0.2, random_state=123456) 33 | return x_train, x_test, y_train, y_test, filelist 34 | 35 | 36 | 37 | def load_data(): 38 | x_train, x_test, y_train, y_test, filelist = load_train_test_data() 39 | print(x_train.shape, y_train.shape) 40 | x_train = x_train.astype('float32') 41 | x_test = x_test.astype('float32') 42 | # x_train /= 255.0 43 | # x_test /= 255.0 44 | input_shape = x_train.shape[1:] # (80, 80) 45 | digit_indices = [np.where(y_train == filelist[i])[0] for i in range(num_classes)] 46 | tr_pairs, tr_y = create_pairs(x_train, digit_indices) 47 | digit_indices = [np.where(y_test == filelist[i])[0] for i in range(num_classes)] 48 | te_pairs, te_y = create_pairs(x_test, digit_indices) 49 | # print(te_pairs.shape, te_y.shape) # (980, 2, 80, 80) (980,) 50 | return input_shape, tr_pairs, tr_y, te_pairs, te_y 51 | 52 | 53 | # predict data with model 54 | def predict(model_path, image_path1, image_path2, target_size): 55 | saved_model = load_model(model_path, custom_objects={'contrastive_loss': contrastive_loss}) 56 | image1 = cv2.imread(image_path1) 57 | image2 = cv2.imread(image_path2) 58 | # 灰度化,并调整尺寸 59 | image1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY) 60 | image1 = cv2.resize(image1, target_size) 61 | image2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY) 62 | image2 = cv2.resize(image2, target_size) # 63 | # print(image2.shape) # (80, 80) 64 | # print(image2) 65 | # 对图像数据做scale操作 66 | data1 = np.array([image1], dtype='float') / 255.0 / 255.0 67 | data2 = np.array([image2], dtype='float') / 255.0 / 255.0 68 | print(data1.shape, data2.shape) # (1, 80, 80) (1, 80, 80) 69 | pairs = np.array([data1, data2]) 70 | print(pairs.shape) # (2, 80, 80) 71 | 72 | y_pred = saved_model.predict([data1, data2]) 73 | print(y_pred) 74 | # print(y_pred) # [[4.1023154]] 75 | # pred = y_pred.ravel() < 0.5 76 | # print(pred) # 如果没有 <0.5则为 [4.1023154] 有的话则是 [False] 77 | # y_true = [1] # 1表示两个是一个类,0表示不同的类 78 | # if pred == y_true: 79 | # print("是同一类") 80 | # else: 81 | # print("不是同一类") 82 | 83 | 84 | --------------------------------------------------------------------------------