Esempio n. 1
0
def prepare_darknet_params():
    inputs = Input(shape=(None, None, 3))
    model = Model(inputs, darknet_body(inputs))

    # det_body = DarkNetDet((None, None), 85)
    # model = det_body.get_detection_body()

    with h5py.File('model_data/yolo3_weights.h5', 'r') as f:
        for idx, layer in enumerate(model.layers):
            print(idx, layer.name)
            if len(layer.weights) != 0:
                splited = layer.name.split("_")
                if splited[-1].isdigit():
                    splited[-1] = str(int(splited[-1]) + 1)
                else:
                    splited.append("1")
                h5_name = "_".join(splited)
                weight_names = load_attributes_from_hdf5_group(f[h5_name], 'weight_names')

                weight_values = [np.asarray(f[h5_name][weight_name]) for weight_name in weight_names]
                layer.set_weights(weight_values)

    model.save_weights("yolo_pretrained.h5")
Esempio n. 2
0
class BaseKerasModel(BaseModel):
    model = None
    tensorboard = None
    train_names = ['train_loss', 'train_mse', 'train_mae']
    val_names = ['val_loss', 'val_mse', 'val_mae']
    counter = 0
    inputs = None
    hidden_layer = None
    outputs = None

    def __init__(self,
                 use_default_dense=True,
                 activation='relu',
                 kernel_regularizer=tf.keras.regularizers.l1(0.001)):
        super().__init__()
        if use_default_dense:
            self.activation = activation
            self.kernel_regularizer = kernel_regularizer

    def create_input_layer(self, input_placeholder: BaseInputFormatter):
        """Creates keras model"""
        self.inputs = tf.keras.layers.InputLayer(
            input_shape=input_placeholder.get_input_state_dimension())
        return self.inputs

    def create_hidden_layers(self, input_layer=None):
        if input_layer is None:
            input_layer = self.inputs
        hidden_layer = tf.keras.layers.Dropout(0.3)(input_layer)
        hidden_layer = tf.keras.layers.Dense(
            128,
            kernel_regularizer=self.kernel_regularizer,
            activation=self.activation)(hidden_layer)
        hidden_layer = tf.keras.layers.Dropout(0.4)(hidden_layer)
        hidden_layer = tf.keras.layers.Dense(
            64,
            kernel_regularizer=self.kernel_regularizer,
            activation=self.activation)(hidden_layer)
        hidden_layer = tf.keras.layers.Dropout(0.3)(hidden_layer)
        hidden_layer = tf.keras.layers.Dense(
            32,
            kernel_regularizer=self.kernel_regularizer,
            activation=self.activation)(hidden_layer)
        hidden_layer = tf.keras.layers.Dropout(0.1)(hidden_layer)
        self.hidden_layer = hidden_layer
        return self.hidden_layer

    def create_output_layer(self,
                            output_formatter: BaseOutputFormatter,
                            hidden_layer=None):
        # sigmoid/tanh all you want on self.model
        if hidden_layer is None:
            hidden_layer = self.hidden_layer
        self.outputs = tf.keras.layers.Dense(
            output_formatter.get_model_output_dimension()[0],
            activation='tanh')(hidden_layer)
        self.model = Model(inputs=self.inputs, outputs=self.outputs)
        return self.outputs

    def write_log(self, callback, names, logs, batch_no, eval=False):
        for name, value in zip(names, logs):
            summary = tf.Summary()
            summary_value = summary.value.add()
            summary_value.simple_value = value
            tag_name = name
            if eval:
                tag_name = 'eval_' + tag_name
            summary_value.tag = tag_name
            callback.writer.add_summary(summary, batch_no)
            callback.writer.flush()

    def finalize_model(self, logname=str(int(random() * 1000))):

        loss, loss_weights = self.create_loss()
        self.model.compile(tf.keras.optimizers.Nadam(lr=0.001),
                           loss=loss,
                           loss_weights=loss_weights,
                           metrics=[
                               tf.keras.metrics.mean_absolute_error,
                               tf.keras.metrics.binary_accuracy
                           ])
        log_name = './logs/' + logname
        self.logger.info("log_name: " + log_name)
        self.tensorboard = tf.keras.callbacks.TensorBoard(
            log_dir=log_name,
            histogram_freq=1,
            write_images=False,
            batch_size=1000,
        )
        self.tensorboard.set_model(self.model)
        self.logger.info("Model has been finalized")

    def fit(self, x, y, batch_size=1):
        if self.counter % 200 == 0:
            logs = self.model.evaluate(x, y, batch_size=batch_size, verbose=1)
            self.write_log(self.tensorboard,
                           self.model.metrics_names,
                           logs,
                           self.counter,
                           eval=True)
            print('step:', self.counter)
        else:
            logs = self.model.train_on_batch(x, y)
            self.write_log(self.tensorboard, self.model.metrics_names, logs,
                           self.counter)
        self.counter += 1

    def predict(self, arr):
        return self.model.predict(arr)

    def save(self, file_path):
        self.model.save_weights(filepath=file_path, overwrite=True)

    def load(self, file_path):
        path = os.path.abspath(file_path)
        self.model.load_weights(filepath=os.path.abspath(file_path))

    def create_loss(self):
        return 'mean_absolute_error', None
Esempio n. 3
0
class jyHEDModelV2_2_SGD_GradientTape_L1(jyModelBase):
    def __init__(self):
        super(jyHEDModelV2_2_SGD_GradientTape_L1, self).__init__()
        self.__listLayerName = []
        self.__pVisualModel = None
        self.__bLoadModel = False
        self.__pTrainFW = tf.summary.create_file_writer(self._strLogPath +
                                                        '/train')
        self.__pValidFW = tf.summary.create_file_writer(self._strLogPath +
                                                        '/valid')
        self.__pMetricsFW = tf.summary.create_file_writer(self._strLogPath +
                                                          '/metrics')

    def structureModel(self):
        weightDecay = 0.00001
        Inputs = layers.Input(shape=self._inputShape,
                              batch_size=self._iBatchSize)
        Con1 = layers.Conv2D(64, (3, 3),
                             name='Con1',
                             activation='relu',
                             padding='SAME',
                             input_shape=self._inputShape,
                             strides=1,
                             kernel_regularizer=l2(weightDecay))(Inputs)
        Con2 = layers.Conv2D(64, (3, 3),
                             name='Con2',
                             activation='relu',
                             padding='SAME',
                             strides=1,
                             kernel_regularizer=l2(weightDecay))(Con1)
        Side1 = sideBranch(Con2, 1)
        MaxPooling1 = layers.MaxPooling2D((2, 2),
                                          name='MaxPooling1',
                                          strides=2,
                                          padding='SAME')(Con2)
        # outputs1
        Con3 = layers.Conv2D(128, (3, 3),
                             name='Con3',
                             activation='relu',
                             padding='SAME',
                             strides=1,
                             kernel_regularizer=l2(weightDecay))(MaxPooling1)
        Con4 = layers.Conv2D(128, (3, 3),
                             name='Con4',
                             activation='relu',
                             padding='SAME',
                             strides=1,
                             kernel_regularizer=l2(weightDecay))(Con3)
        Side2 = sideBranch(Con4, 2)
        MaxPooling2 = layers.MaxPooling2D((2, 2),
                                          name='MaxPooling2',
                                          strides=2,
                                          padding='SAME')(Con4)
        # outputs2
        Con5 = layers.Conv2D(256, (3, 3),
                             name='Con5',
                             activation='relu',
                             padding='SAME',
                             strides=1,
                             kernel_regularizer=l2(weightDecay))(MaxPooling2)
        Con6 = layers.Conv2D(256, (3, 3),
                             name='Con6',
                             activation='relu',
                             padding='SAME',
                             strides=1,
                             kernel_regularizer=l2(weightDecay))(Con5)
        Con7 = layers.Conv2D(256, (3, 3),
                             name='Con7',
                             activation='relu',
                             padding='SAME',
                             strides=1,
                             kernel_regularizer=l2(weightDecay))(Con6)
        Side3 = sideBranch(Con7, 4)
        MaxPooling3 = layers.MaxPooling2D((2, 2),
                                          name='MaxPooling3',
                                          strides=2,
                                          padding='SAME')(Con7)
        # outputs3
        Con8 = layers.Conv2D(512, (3, 3),
                             name='Con8',
                             activation='relu',
                             padding='SAME',
                             strides=1,
                             kernel_regularizer=l2(weightDecay))(MaxPooling3)
        Con9 = layers.Conv2D(512, (3, 3),
                             name='Con9',
                             activation='relu',
                             padding='SAME',
                             strides=1,
                             kernel_regularizer=l2(weightDecay))(Con8)
        Con10 = layers.Conv2D(512, (3, 3),
                              name='Con10',
                              activation='relu',
                              padding='SAME',
                              strides=1,
                              kernel_regularizer=l2(weightDecay))(Con9)
        Side4 = sideBranch(Con10, 8)
        MaxPooling4 = layers.MaxPooling2D((2, 2),
                                          name='MaxPooling4',
                                          strides=2,
                                          padding='SAME')(Con10)
        # outputs4
        Con11 = layers.Conv2D(512, (3, 3),
                              name='Con11',
                              activation='relu',
                              padding='SAME',
                              strides=1,
                              kernel_regularizer=l2(weightDecay))(MaxPooling4)
        Con12 = layers.Conv2D(512, (3, 3),
                              name='Con12',
                              activation='relu',
                              padding='SAME',
                              strides=1,
                              kernel_regularizer=l2(weightDecay))(Con11)
        Con13 = layers.Conv2D(512, (3, 3),
                              name='Con13',
                              activation='relu',
                              padding='SAME',
                              strides=1,
                              kernel_regularizer=l2(weightDecay))(Con12)
        Side5 = sideBranch(Con13, 16)
        Fuse = layers.Concatenate(axis=-1)([Side1, Side2, Side3, Side4, Side5])

        # learn fusion weight
        fuseInitWeight = initializers.constant(0.2)
        Fuse = layers.Conv2D(1, (1, 1),
                             name='Fuse',
                             padding='SAME',
                             use_bias=False,
                             activation=None,
                             kernel_initializer=fuseInitWeight,
                             kernel_regularizer=l1(weightDecay))(Fuse)

        # output1 = layers.Activation('sigmoid', name='output1')(Side1)
        # output2 = layers.Activation('sigmoid', name='output2')(Side2)
        # output3 = layers.Activation('sigmoid', name='output3')(Side3)
        # output4 = layers.Activation('sigmoid', name='output4')(Side4)
        # output5 = layers.Activation('sigmoid', name='output5')(Side5)
        output6 = layers.Activation('sigmoid', name='output6')(Fuse)

        outputs = [output6
                   ]  # [output1, output2, output3, output4, output5, output6]
        self._pModel = Model(inputs=Inputs, outputs=outputs)
        pOptimizer = optimizers.adam(lr=0.0001)
        pOptimizer = optimizers.SGD(lr=0.000001, decay=0., momentum=0.9)
        pOptimizer = tf.optimizers.SGD(lr=0.5, decay=0., momentum=0.9)
        # pOptimizer = monitorSGD(lr=0.000001, decay=0., momentum=0.9)
        # grads = tf.gradients(classBalancedSigmoidCrossEntropy, self._pModel.trainable_weights)
        # pSGD = optimizers.SGD()

        self._pModel.compile(
            loss={
                # 'output1': classBalancedSigmoidCrossEntropy,
                # 'output2': classBalancedSigmoidCrossEntropy,
                # 'output3': classBalancedSigmoidCrossEntropy,
                # 'output4': classBalancedSigmoidCrossEntropy,
                # 'output5': classBalancedSigmoidCrossEntropy,
                'output6': classBalancedSigmoidCrossEntropy
            },
            optimizer=pOptimizer)

        # self._pModel.summary()

    def startTrain(self, listDS, iMaxLen, iBatchSize):
        '''
        itrTrain = tf.compat.v1.data.make_one_shot_iterator(listDS[0])
        itrValid = tf.compat.v1.data.make_one_shot_iterator(listDS[1])

        iStepsPerEpochTrain = int(iMaxLen[0] / iBatchSize[0])
        iStepsPerEpochValid = int(iMaxLen[1] / iBatchSize[1])

        pBack = myCallback(self._strLogPath)

        self._pModel.fit(itrTrain, validation_data=itrValid, epochs=self._iEpochs,
                         callbacks=[self._pSaveModel, self._pTensorboard, pBack], steps_per_epoch=iStepsPerEpochTrain,
                         validation_steps=iStepsPerEpochValid)
        '''
        itrTrain = tf.compat.v1.data.make_one_shot_iterator(listDS[0])
        itrValid = tf.compat.v1.data.make_one_shot_iterator(listDS[1])
        iStepsPerEpochTrain = int(iMaxLen[0] / iBatchSize[0])
        iStepsPerEpochValid = int(iMaxLen[1] / iBatchSize[1])

        # trainLoss = tf.keras.metrics.Mean(name='train_loss')
        dictLossGroup = self._pModel.loss
        # t = self._pModel.layers[23].losses
        # t = self._pModel.weights[0].name
        # p = self._pModel.loss
        iTick = 0

        # epoch
        for epoch in range(self._iEpochs):
            # save model
            if iTick > self._iPeriod:
                strModelFileName = self._strModelFileName.format(epoch=epoch +
                                                                 1)
                filepath = self._strSavePath + strModelFileName
                print(self._strFormat %
                      ('Epoch: %s/%s, SaveModel: %s' %
                       (str(epoch), str(self._iEpochs), strModelFileName)))
                self._pModel.save_weights(filepath, overwrite=True)
                iTick = 0
            iTick += 1
            # stepsPerEpoch
            for stepsPerEpoch in range(iStepsPerEpochTrain):
                with tf.GradientTape() as tape:
                    itr = itrTrain.next()
                    # output define as [out1, out2, ....., out6]
                    listPredict = [self._pModel(itr[0])]
                    t = self._pModel.weights
                    listLabel = [itr[1]]

                    listLoss = []
                    fAllLoss = 0.
                    template = 'Per: {}/{}, TrainLoss: {}  --  '
                    i = 0

                    # multiple output, calculate loss
                    for key in dictLossGroup:
                        # loss function
                        pLoss = dictLossGroup[key]
                        # add regularize
                        regularization_loss = tf.math.add_n(
                            self._pModel.losses)
                        # pLoss += tf.add_n
                        # loss value
                        outputLoss = pLoss(
                            listLabel[i], listPredict[i]) + regularization_loss
                        listLoss.append(outputLoss)
                        # sum of loss
                        fAllLoss += outputLoss
                        # print format
                        template += 'train_loss_%s: {}  --  ' % key
                        i += 1

                    # calculate gradient
                    gradient = tape.gradient(fAllLoss,
                                             self._pModel.trainable_weights)

                    # trainLoss(fAllLoss)
                    template += '\n'
                    print(
                        template.format(stepsPerEpoch + 1, iStepsPerEpochTrain,
                                        fAllLoss, listLoss[0]))
                    # backprop
                    self._pModel.optimizer.apply_gradients(
                        zip(gradient, self._pModel.trainable_weights))

            # 每执行完一个train epoch 进行validcross 因此valid计算不能与train同步进行要在train epoch结束后进行
            fValidAllLoss = 0.
            listValidLoss = list(0 for n in range(len(dictLossGroup)))
            for stepsPerEpochValid in range(iStepsPerEpochValid):
                itr2 = itrValid.next()
                listPreValid = [self._pModel(itr2[0])]
                listValidLabel = [itr2[1]]
                i = 0
                for key in dictLossGroup:
                    # loss function
                    pLoss = dictLossGroup[key]
                    # loss value
                    outputValidLoss = pLoss(listValidLabel[i], listPreValid[i])
                    listValidLoss[i] += outputValidLoss
                    # sum of loss
                    fValidAllLoss += outputValidLoss
                    # print format
                    # template += '  --train_loss_%s: {}--  ' % key
                    i += 1

            # mean of val_loss
            fValidAllLoss /= iStepsPerEpochValid
            validTemplate = 'Epoch {}, val_loss: {}  --  '.format(
                epoch + 1, fValidAllLoss)
            for k in range(len(listValidLoss)):
                listValidLoss[k] /= iStepsPerEpochValid
                validTemplate += 'val_loss_{}: {}  --  '.format(
                    k + 1, listValidLoss[k])

            print(
                '\n-----------------------------------------------------------------------\n'
            )
            print(validTemplate)
            print(
                '\n-----------------------------------------------------------------------\n'
            )

            # per epoch output
            with self.__pTrainFW.as_default():
                i = 0
                tf.summary.scalar('loss: ', fAllLoss, step=epoch)
                # tf.summary.scalar('val_loss: ', fValidAllLoss, step=epoch)

                for key in dictLossGroup:
                    tf.summary.scalar('loss_' + key, listLoss[i], step=epoch)
                    # tf.summary.scalar('val_loss_' + key, listValidLoss[i], step=epoch)
                    i += 1

            with self.__pMetricsFW.as_default():
                # save gradient each layer
                pLayerWeight = self._pModel.trainable_weights
                for i in range(len(pLayerWeight)):
                    strName = pLayerWeight[i].name + '/Grad'
                    tf.summary.histogram(strName, gradient[i], step=epoch)
                    # mean grad
                    meanGrad = tf.reduce_mean(gradient[i])
                    tf.summary.scalar(strName + '/Mean', meanGrad, step=epoch)
                    # model grad
                    tensorNorm = tf.norm(gradient[i])
                    tf.summary.scalar(strName + '/Norm',
                                      tensorNorm,
                                      step=epoch)

            with self.__pValidFW.as_default():
                i = 0
                tf.summary.scalar('loss: ', fValidAllLoss, step=epoch)

                for key in dictLossGroup:
                    tf.summary.scalar('loss_' + key,
                                      listValidLoss[i],
                                      step=epoch)
                    i += 1

    def loadWeights(self, strPath):
        # last = tf.train.latest_checkpoint(strPath)
        # checkPoint = tf.train.load_checkpoint(strPath)
        self._pModel.load_weights(strPath)
        # w = self._pModel.weights
        # visual model
        self.__bLoadModel = True

    def generateVisualModel(self):
        outputs = []
        for myLayer in self._pModel.layers:
            self.__listLayerName.append(myLayer.name)
            outputs.append(myLayer.output)

        # print(self.__pModel.layers[0])
        # self.__pVisualModel = Model(self.__pModel.inputs, outputs=outputs)
        self.__pVisualModel = Model(self._pModel.inputs,
                                    outputs=self._pModel.outputs)
        return self.__pVisualModel

    def predict(self, IMG):
        # pImage = open(IMG, 'rb').read()
        # tensorIMG = tf.image.decode_jpeg(pImage)
        pIMG = image.array_to_img(IMG)  # .resize((256, 144))
        tensorIMG = image.img_to_array(pIMG)
        x = np.array(tensorIMG / 255.0)
        # show image
        iColumn = 4
        # generate window
        plt.figure(num='Input')
        # plt.subplot(1, 1, 1)
        plt.imshow(x)

        # imagetest = x

        x = np.expand_dims(x, axis=0)
        # pyplot.imshow(x)
        time1 = datetime.datetime.now()
        outputs = self.__pVisualModel.predict(x)
        time2 = datetime.datetime.now()
        print(time2 - time1)
        i = 100
        listOutput = []
        for i in range(len(outputs)):
            outputShape = outputs[i].shape
            singleOut = outputs[i].reshape(outputShape[0], outputShape[1],
                                           outputShape[2])
            # singleOut *= 255
            listOutput.append(singleOut)
        singleOut = listOutput[-1]
        singleOut[singleOut > 0.5] = 1
        listOutput[-1] = singleOut
        return listOutput
        '''
        for output in outputs:
            # plt.figure(num='%s' % str(i))
            outputShape = output.shape
            singleOut = output.reshape(outputShape[1], outputShape[2], outputShape[3])
            singleOut *= 255
            if outputShape[3] == 1:
                # test = x - output
                # test = np.abs(test)
                # return mysum

                # plt.subplot(1, 1, 1)
                # plt.imshow(singleOut, camp='gray')
                # cv2.imwrite('D:\wyc\Projects\TrainDataSet\HED\Result/%s.jpg' % str(i), singleOut)
                return singleOut
                # i += 1
                # plt.show()
        '''

    def getModelConfig(self):
        return self._iBatchSize
        best_metric = float("inf")
        wait = 0
        for epoch in range(args.epoches):
            printf("training epoch %s..." % (epoch, ))
            train_loss, train_ci = loop_dataset(train_inds,
                                                optimizer=optimizer)
            printf("train epoch %d loss %.4f ci %.4f \n" %
                   (epoch, train_loss, train_ci))

            printf("validating epoch %s..." % (epoch, ))
            val_loss, val_ci = loop_dataset(val_inds, optimizer=None)
            printf("validating epoch %d loss %.4f ci %.4f \n" %
                   (epoch, val_loss, val_ci))
            if val_loss <= best_metric:
                best_metric = val_loss
                DTAModel.save_weights(os.path.join(chkpt_subdir, "DTA"), )
                wait = 0
            else:
                wait += 1

            if wait > args.patience:
                break

    DTAModel.load_weights(os.path.join(chkpt_subdir, "DTA"))

    printf("start testing...")
    test_loss, test_ci = loop_dataset(test_inds, optimizer=None)
    if test_ci > best_ci:
        best_ci = test_ci
        best_loss = test_loss
        best_it = it
Esempio n. 5
0
    kl_loss *= -0.5
    vae_loss = K.mean(reconstruction_loss + kl_loss)
    vae.add_loss(vae_loss)
    vae.compile(optimizer='adam')
    vae.summary()
    plot_model(vae, to_file='vae_mlp.png', show_shapes=True)

    if args.weights:
        vae.load_weights(args.weights)
    else:
        # train the autoencoder
        vae.fit(x_train,
                epochs=epochs,
                batch_size=batch_size,
                validation_data=(x_test, None))
        vae.save_weights('vae_mlp_mnist.h5')

    plot_results(models, data, batch_size=batch_size, model_name="vae_mlp")

    # # plot the latent space of the encoded digits #########################################
    # x_test_encoded = encoder.predict(x_test, batch_size=batch_size)
    # plt.figure(figsize=(6, 6))
    # plt.scatter(x_test_encoded[:, 0], x_test_encoded[:, 1], c=y_test)
    # plt.colorbar()
    # plt.show()
    #
    # # scan the latent plane and generate new digits #########################################
    # # display a 2D manifold of the digits
    # n = 15  # figure with 15x15 digits
    # digit_size = 28
    # figure = np.zeros((digit_size * n, digit_size * n))