def create_pb_model_of_pretrained(Net):
    K.set_learning_phase(0)
    #if not(Net=='VGG'):
    tf.reset_default_graph()
    with K.get_session().as_default():
        if Net == 'InceptionV1_slim':
            model = InceptionV1_slim(include_top=True, weights='imagenet')
            name = "tf_inception_v1_slim.pb"
        elif Net == 'InceptionV1':
            model = inception_v1_oldTF(weights='imagenet', include_top=True)
            name = "tf_inception_v1.pb"
        elif Net == 'VGG':
            model = tf.keras.applications.vgg19.VGG19(include_top=False, weights='imagenet',\
                                                      input_shape=(224,224,3))
            name = "tf_vgg19.pb"
        elif Net == 'ResNet50':
            model = tf.keras.applications.resnet50.ResNet50(include_top=True, weights='imagenet',\
                                                          input_shape= (224, 224, 3))
            name = "tf_resnet50.pb"
        else:
            raise (ValueError(Net + ' is unknown'))

        os.makedirs('./model', exist_ok=True)

        frozen_graph = freeze_session(
            K.get_session(),
            output_names=[out.op.name for out in model.outputs],
            clear_devices=True)
        # Save the pb model
        tf.io.write_graph(frozen_graph,
                          logdir="model",
                          name=name,
                          as_text=False)
def get_pretrained_model(Net, include_top=True):
    if Net == 'InceptionV1_slim':
        model = InceptionV1_slim(include_top=include_top, weights='imagenet')
    elif Net == 'InceptionV1':
        model = inception_v1_oldTF(weights='imagenet', include_top=include_top)
    elif Net == 'VGG':
        model = tf.keras.applications.vgg19.VGG19(include_top=include_top,
                                                  weights='imagenet',
                                                  input_shape=(224, 224, 3))
    elif Net == 'ResNet50':
        model = tf.keras.applications.resnet50.ResNet50(include_top=include_top, weights='imagenet',\
                                                      input_shape= (224, 224, 3))
    else:
        raise (ValueError(Net + ' is unknown'))

    return (model)
Beispiel #3
0
def get_dico_layers_type_all_layers(Net):
    if Net == 'InceptionV1_slim':
        model = InceptionV1_slim(include_top=True, weights='imagenet')
    elif Net == 'InceptionV1':
        model = inception_v1_oldTF(weights='imagenet', include_top=True)
    elif Net == 'VGG':
        model = tf.keras.applications.vgg19.VGG19(include_top=False,
                                                  weights='imagenet',
                                                  input_shape=(224, 224, 3))
    elif Net == 'ResNet50':
        model = tf.keras.applications.resnet50.ResNet50(include_top=True, weights='imagenet',\
                                                      input_shape= (224, 224, 3))
    else:
        raise (ValueError(Net + ' is unknown'))

    dico = get_dico_layers_type_all_layers_fromNet(model)

    return (dico)
def test_autocorr_render_Inception_v1():

    tf.reset_default_graph()
    K.set_learning_phase(0)
    if not (os.path.isfile("model/tf_inception_v1.pb")):
        with K.get_session().as_default():
            model = inception_v1_oldTF(
                weights='imagenet',
                include_top=True)  #include_top=True, weights='imagenet')
            print(model.input)
            os.makedirs('./model', exist_ok=True)

            frozen_graph = freeze_session(
                K.get_session(),
                output_names=[out.op.name for out in model.outputs],
                clear_devices=True)
            # Save the pb model
            tf.io.write_graph(frozen_graph,
                              logdir="model",
                              name="tf_inception_v1.pb",
                              as_text=False)
            nodes_tab = [
                n.name for n in tf.get_default_graph().as_graph_def().node
            ]
            print(nodes_tab)

    #with tf.Graph().as_default() as graph, tf.Session() as sess:
    with gradient_override_map({
            'Relu': redirected_relu_grad,
            'ReLU': redirected_relu_grad
    }):
        lucid_inception_v1 = Lucid_InceptionV1()
        lucid_inception_v1.load_graphdef()


    out = render.render_vis(lucid_inception_v1, 'mixed4a_1x1_pre_relu/Conv2D:0',\
                            relu_gradient_override=True,use_fixed_seed=True)
    plt.figure()
    plt.imshow(out[0][0])


    out = render.render_vis(lucid_inception_v1, 'mixed4b_pre_relu/concat:452',\
                            relu_gradient_override=True,use_fixed_seed=True)
    plt.figure()
    plt.imshow(out[0][0])

    JITTER = 1
    ROTATE = 5
    SCALE = 1.1

    transforms = [
        transform.pad(2 * JITTER),
        transform.jitter(JITTER),
        transform.random_scale([SCALE**(n / 10.) for n in range(-10, 11)]),
        transform.random_rotate(range(-ROTATE, ROTATE + 1))
    ]

    imgs = render.render_vis(lucid_inception_v1,
                             'mixed4b_pre_relu/concat:452',
                             transforms=transforms,
                             param_f=lambda: param.image(64),
                             thresholds=[2048],
                             verbose=False,
                             relu_gradient_override=True,
                             use_fixed_seed=True)
    plt.figure()
    plt.imshow(imgs[0][0])