Ejemplo n.º 1
0
    def build_model(self):
        if self.model_name == 'VGG16':
            model = keras.applications.VGG16(include_top=True,
                                                   weights=None,
                                                   input_tensor=None,
                                                   input_shape=(self.normal_size,self.normal_size,self.channles),
                                                   pooling='max',
                                                   classes=self.classes)
        elif self.model_name == 'VGG19':
            model = keras.applications.VGG19(include_top=True,
                                                   weights=None,
                                                   input_tensor=None,
                                                   input_shape=(self.normal_size,self.normal_size,self.channles),
                                                   pooling='max',
                                                   classes=self.classes)

        elif self.model_name == 'ResNet50':
            model = keras.applications.ResNet50(include_top=True,
                                                   weights=None,
                                                   input_tensor=None,
                                                   input_shape=(self.normal_size,self.normal_size,self.channles),
                                                   pooling='max',
                                                   classes=self.classes)
        elif self.model_name == 'InceptionV3':
            model = keras.applications.InceptionV3(include_top=True,
                                                   weights=None,
                                                   input_tensor=None,
                                                   input_shape=(self.normal_size,self.normal_size,self.channles),
                                                   pooling='max',
                                                   classes=self.classes)

        elif self.model_name == 'Xception':
            model = keras.applications.Xception(include_top=True,
                                                weights=None,
                                                input_tensor=None,
                                                input_shape=(self.normal_size,self.normal_size,self.channles),
                                                pooling='max',
                                                classes=self.classes)
        elif self.model_name == 'MobileNet':
            model = keras.applications.MobileNet(include_top=True,
                                                 weights=None,
                                                 input_tensor=None,
                                                 input_shape=(self.normal_size,self.normal_size,self.channles),
                                                 pooling='max',
                                                 classes=self.classes)
        elif self.model_name == 'DenseNet':
            depth = 40
            nb_dense_block = 3
            growth_rate = 12
            nb_filter = 12
            bottleneck = False
            reduction = 0.0
            dropout_rate = 0.0

            img_dim = (self.channles, self.normal_size) if K.image_dim_ordering() == "th" else (
                self.normal_size, self.normal_size, self.channles)

            model = densenet.DenseNet(img_dim, classes=self.classes, depth=depth, nb_dense_block=nb_dense_block,
                                      growth_rate=growth_rate, nb_filter=nb_filter, dropout_rate=dropout_rate,
                                      bottleneck=bottleneck, reduction=reduction, weights=None)

        elif self.model_name == 'AlexNet':
            model = MODEL(self.config).AlexNet()
        elif self.model_name == 'LeNet':
            model = MODEL(self.config).LeNet()
        elif self.model_name == 'ZF_Net':
            model = MODEL(self.config).ZF_Net()
        elif self.model_name == 'ResNet18':
            model = ResnetBuilder().build_resnet18(self.config)
        elif self.model_name == 'ResNet34':
            model = ResnetBuilder().build_resnet34(self.config)
        elif self.model_name == 'ResNet101':
            model = ResnetBuilder().build_resnet101(self.config)
        elif self.model_name == 'ResNet152':
            model = ResnetBuilder().build_resnet152(self.config)

        if self.default_optimizers:
            adam = keras.optimizers.Adam(lr=self.lr, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0)
            model.compile(loss="categorical_crossentropy", optimizer=adam, metrics=["accuracy"])  # compile之后才会更新权重和模型
        else:
            model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"])


        return model
    def model_confirm(self, choosed_model):
        if choosed_model == 'VGG16':
            model = MODEL(self.config).VGG16()
        elif choosed_model == 'VGG19':
            model = MODEL(self.config).VGG19()
        elif choosed_model == 'AlexNet':
            model = MODEL(self.config).AlexNet()
        elif choosed_model == 'LeNet':
            model = MODEL(self.config).LeNet()
        elif choosed_model == 'ZF_Net':
            model = MODEL(self.config).ZF_Net()
        elif choosed_model == 'ResNet18':
            model = ResnetBuilder().build_resnet18(self.config)
        elif choosed_model == 'ResNet34':
            model = ResnetBuilder().build_resnet34(self.config)
        elif choosed_model == 'ResNet101':
            model = ResnetBuilder().build_resnet101(self.config)
        elif choosed_model == 'ResNet152':
            model = ResnetBuilder().build_resnet152(self.config)
        elif choosed_model == 'mnist_net':
            model = MODEL(self.config).mnist_net()
        elif choosed_model == 'TSL16':
            model = MODEL(self.config).TSL16()
        elif choosed_model == 'ResNet50':
            model = keras.applications.ResNet50(include_top=True,
                                                weights=None,
                                                input_tensor=None,
                                                input_shape=(self.normal_size,
                                                             self.normal_size,
                                                             self.channles),
                                                pooling='max',
                                                classes=self.classNumber)
        elif choosed_model == 'InceptionV3':
            model = keras.applications.InceptionV3(
                include_top=True,
                weights=None,
                input_tensor=None,
                input_shape=(self.normal_size, self.normal_size,
                             self.channles),
                pooling='max',
                classes=self.classNumber)

        elif choosed_model == 'Xception':
            model = keras.applications.Xception(include_top=True,
                                                weights=None,
                                                input_tensor=None,
                                                input_shape=(self.normal_size,
                                                             self.normal_size,
                                                             self.channles),
                                                pooling='max',
                                                classes=self.classNumber)
        elif choosed_model == 'MobileNet':
            model = keras.applications.MobileNet(include_top=True,
                                                 weights=None,
                                                 input_tensor=None,
                                                 input_shape=(self.normal_size,
                                                              self.normal_size,
                                                              self.channles),
                                                 pooling='max',
                                                 classes=self.classNumber)
        elif choosed_model == 'InceptionResNetV2':
            model = keras.applications.InceptionResNetV2(
                include_top=True,
                weights=None,
                input_tensor=None,
                input_shape=(self.normal_size, self.normal_size,
                             self.channles),
                pooling='max',
                classes=self.classNumber)
        elif choosed_model == 'SEResNetXt':
            model = SEResNetXt(self.config).model

        elif choosed_model == 'DenseNet':
            depth = 40
            nb_dense_block = 3
            growth_rate = 12
            nb_filter = 12
            bottleneck = False
            reduction = 0.0
            dropout_rate = 0.0

            img_dim = (self.channles, self.normal_size
                       ) if K.image_data_format == 'channels_last' else (
                           self.normal_size, self.normal_size, self.channles)

            model = densenet.DenseNet(img_dim,
                                      classNumber=self.classNumber,
                                      depth=depth,
                                      nb_dense_block=nb_dense_block,
                                      growth_rate=growth_rate,
                                      nb_filter=nb_filter,
                                      dropout_rate=dropout_rate,
                                      bottleneck=bottleneck,
                                      reduction=reduction,
                                      weights=None)

        elif choosed_model == 'SENet':
            model = sm.Unet('senet154',
                            input_shape=(self.normal_size, self.normal_size,
                                         self.channles),
                            classes=4,
                            activation='softmax',
                            encoder_weights=None)

            #model.summary()

        elif choosed_model == 'EfficientNetB5':
            model = EfficientNetB5(input_shape=(self.normal_size,
                                                self.normal_size,
                                                self.channles),
                                   classes=4,
                                   weights=None)

        elif choosed_model == 'EfficientNetB4':
            model = EfficientNetB4(input_shape=(self.normal_size,
                                                self.normal_size,
                                                self.channles),
                                   classes=4,
                                   weights=None)

        elif choosed_model == 'EfficientNetB3':
            model = EfficientNetB3(input_shape=(self.normal_size,
                                                self.normal_size,
                                                self.channles),
                                   classes=4,
                                   weights=None)

        elif choosed_model == 'EfficientNetB2':
            model = EfficientNetB2(input_shape=(self.normal_size,
                                                self.normal_size,
                                                self.channles),
                                   classes=4,
                                   weights=None)

        elif choosed_model == 'EfficientNetB1':
            model = EfficientNetB1(input_shape=(self.normal_size,
                                                self.normal_size,
                                                self.channles),
                                   classes=4,
                                   weights=None)

        elif choosed_model == 'EfficientNetB0':
            model = EfficientNetB0(input_shape=(self.normal_size,
                                                self.normal_size,
                                                self.channles),
                                   classes=4,
                                   weights=None)

        elif choosed_model == 'MobileNetV3_Large':
            model = MobileNetV3_Large(shape=(self.normal_size,
                                             self.normal_size, self.channles),
                                      n_class=4).build()

        elif choosed_model == 'MobileNetV3_Small':
            model = MobileNetV3_Small(shape=(self.normal_size,
                                             self.normal_size, self.channles),
                                      n_class=4).build()

        elif choosed_model == 'NASNetLarge':
            model = NASNetLarge(input_shape=(self.normal_size,
                                             self.normal_size, self.channles),
                                weights=None,
                                use_auxiliary_branch=False,
                                classes=4)

        elif choosed_model == 'NASNetMobile':
            model = NASNetMobile(input_shape=(self.normal_size,
                                              self.normal_size, self.channles),
                                 weights=None,
                                 use_auxiliary_branch=False,
                                 classes=4)

        elif choosed_model == 'NASNetMiddle':
            model = NASNetMiddle(input_shape=(self.normal_size,
                                              self.normal_size, self.channles),
                                 weights=None,
                                 use_auxiliary_branch=False,
                                 classes=4)

        elif choosed_model == 'ShuffleNet':
            model = ShuffleNet(input_shape=(self.normal_size, self.normal_size,
                                            self.channles),
                               classes=4)

        elif choosed_model == 'ShuffleNetV2':
            model = ShuffleNetV2(input_shape=(self.normal_size,
                                              self.normal_size, self.channles),
                                 classes=4)

        return model
Ejemplo n.º 3
0
    def model_confirm(self, choosed_model):
        if choosed_model == 'VGG16':
            model = MODEL(self.config).VGG16()
        elif choosed_model == 'VGG19':
            model = MODEL(self.config).VGG19()
        elif choosed_model == 'AlexNet':
            model = MODEL(self.config).AlexNet()
        elif choosed_model == 'LeNet':
            model = MODEL(self.config).LeNet()
        elif choosed_model == 'ZF_Net':
            model = MODEL(self.config).ZF_Net()
        elif choosed_model == 'ResNet18':
            model = ResnetBuilder().build_resnet18(self.config)
        elif choosed_model == 'ResNet34':
            model = ResnetBuilder().build_resnet34(self.config)
        elif choosed_model == 'ResNet101':
            model = ResnetBuilder().build_resnet101(self.config)
        elif choosed_model == 'ResNet152':
            model = ResnetBuilder().build_resnet152(self.config)
        elif choosed_model == 'mnist_net':
            model = MODEL(self.config).mnist_net()
        elif choosed_model == 'TSL16':
            model = MODEL(self.config).TSL16()

        elif choosed_model == 'ResNet50':
            model = keras.applications.ResNet50(include_top=True,
                                                weights=None,
                                                input_tensor=None,
                                                input_shape=(self.normal_size,
                                                             self.normal_size,
                                                             self.channles),
                                                pooling='max',
                                                classes=self.classes)
        elif choosed_model == 'InceptionV3':
            model = keras.applications.InceptionV3(
                include_top=True,
                weights=None,
                input_tensor=None,
                input_shape=(self.normal_size, self.normal_size,
                             self.channles),
                pooling='max',
                classes=self.classes)

        elif choosed_model == 'Xception':
            model = keras.applications.Xception(include_top=True,
                                                weights=None,
                                                input_tensor=None,
                                                input_shape=(self.normal_size,
                                                             self.normal_size,
                                                             self.channles),
                                                pooling='max',
                                                classes=self.classes)
        elif choosed_model == 'MobileNet':
            model = keras.applications.MobileNet(include_top=True,
                                                 weights=None,
                                                 input_tensor=None,
                                                 input_shape=(self.normal_size,
                                                              self.normal_size,
                                                              self.channles),
                                                 pooling='max',
                                                 classes=self.classes)
        # elif choosed_model == 'DenseNet':
        #     depth = 40
        #     nb_dense_block = 3
        #     growth_rate = 12
        #     nb_filter = 12
        #     bottleneck = False
        #     reduction = 0.0
        #     dropout_rate = 0.0
        #
        #     img_dim = (self.channles, self.normal_size) if K.image_dim_ordering() == "th" else (
        #         self.normal_size, self.normal_size, self.channles)
        #
        #     model = densenet.DenseNet(img_dim, classes=self.classes, depth=depth, nb_dense_block=nb_dense_block,
        #                               growth_rate=growth_rate, nb_filter=nb_filter, dropout_rate=dropout_rate,
        #                               bottleneck=bottleneck, reduction=reduction, weights=None)

        return model
#zero and we can train our model whenever we want to
def gameOver(model,l,Move):
	with open(f'MOVES/model_{l}.txt','w') as file:
		file.write(str(Move))
	model.save()
	pygame.quit()
	sys.exit()


pygame.init()
window = pygame.display.set_mode((600,600))
pygame.display.set_caption("Snake_Game")
fps = pygame.time.Clock()
snakes=[snake() for i in range(1)]
food=FOOD(snakes)
Model = MODEL(57,[20,10],3)
Model.load()
model = Model.model

#move is responsible for snakes move (for tracking down the count training examples )
LAYERS = '[57][20,10][3]'

try:
	with open(f'MOVES/model_{LAYERS}.txt','r') as file:
		move = int(file.read())
except:
	move = 0



while True: