Skip to content

It's not compatible with TensorFlow v2 #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import streamlit as st
import cv2
from PIL import Image
import numpy as np
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
import lpips_tf

def load_image(fname):
image = cv2.imread(fname)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
return image.astype(np.float32) / 255.0

net = st.radio("Pick a model", ['vgg', 'alex', 'squeeze'])
uploaded_file = st.file_uploader("Choose a file", key="0")
uploaded_file1 = st.file_uploader("Choose a file", key="1")
if uploaded_file is not None and uploaded_file1 is not None:
ex_ref = load_image(str(uploaded_file.name))
image = Image.open(uploaded_file)
#col1, col2= st.columns([1, 45])
st.image(uploaded_file, caption='Reference Image', use_column_width=False)
ex_p0 = load_image(str(uploaded_file1.name))
image1 = Image.open(uploaded_file1)
st.image(uploaded_file1, caption='Input Image', use_column_width=False)

if st.button('Get the distance'):
if uploaded_file is not None and uploaded_file1 is not None:
model = 'net-lin'
version = '0.1'
session = tf.Session()
image0_ph = tf.placeholder(tf.float32)
image1_ph = tf.placeholder(tf.float32)
lpips_fn = session.make_callable(
lpips_tf.lpips(image0_ph, image1_ph, model=model, net=net, version=version),
[image0_ph, image1_ph])

ex_d0 = lpips_fn(ex_ref, ex_p0)
output = 'Distances: (%.3f)' % ex_d0
st.write(output)
183 changes: 183 additions & 0 deletions lpips_example.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "lpips_my_repo",
"provenance": [],
"gpuType": "T4"
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
},
"accelerator": "GPU",
"gpuClass": "standard"
},
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "-lty-7hnz7r5",
"outputId": "725243f8-1a35-4802-b392-c913a8273775"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Cloning into 'lpips-tensorflow'...\n",
"remote: Enumerating objects: 76, done.\u001b[K\n",
"remote: Counting objects: 100% (9/9), done.\u001b[K\n",
"remote: Compressing objects: 100% (9/9), done.\u001b[K\n",
"remote: Total 76 (delta 2), reused 0 (delta 0), pack-reused 67\u001b[K\n",
"Unpacking objects: 100% (76/76), 19.69 KiB | 1.09 MiB/s, done.\n"
]
}
],
"source": [
"!git clone https://github.com/ayyucedemirbas/lpips-tensorflow.git"
]
},
{
"cell_type": "code",
"source": [
"%cd lpips-tensorflow"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "JL0RVEr-z-Rk",
"outputId": "0ed1ba59-736d-419f-8426-847755cfcdc0"
},
"execution_count": 2,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"/content/lpips-tensorflow\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"%%writefile test_network.py\n",
"import argparse\n",
"\n",
"import cv2\n",
"import numpy as np\n",
"import tensorflow.compat.v1 as tf\n",
"tf.disable_v2_behavior()\n",
"\n",
"import lpips_tf\n",
"\n",
"\n",
"def load_image(fname):\n",
" image = cv2.imread(fname)\n",
" image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n",
" return image.astype(np.float32) / 255.0\n",
"\n",
"\n",
"def main():\n",
" parser = argparse.ArgumentParser()\n",
" parser.add_argument('--model', choices=['net-lin', 'net'], default='net-lin', help='net-lin or net')\n",
" parser.add_argument('--net', choices=['squeeze', 'alex', 'vgg'], default='alex', help='squeeze, alex, or vgg')\n",
" parser.add_argument('--version', type=str, default='0.1')\n",
" args = parser.parse_args()\n",
"\n",
" ex_ref = load_image('1.jpg')\n",
" ex_p0 = load_image('2.jpg')\n",
"\n",
"\n",
" session = tf.Session()\n",
"\n",
" image0_ph = tf.placeholder(tf.float32)\n",
" image1_ph = tf.placeholder(tf.float32)\n",
" lpips_fn = session.make_callable(\n",
" lpips_tf.lpips(image0_ph, image1_ph, model=args.model, net=args.net, version=args.version),\n",
" [image0_ph, image1_ph])\n",
"\n",
" ex_d0 = lpips_fn(ex_ref, ex_p0)\n",
"\n",
" print('Distances: (%.3f)' % (ex_d0))\n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "wuHHzcsJ0IIi",
"outputId": "75064903-aad5-4422-fee8-c2bb4a238c50"
},
"execution_count": 5,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Overwriting test_network.py\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"!python test_network.py"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "_-cz8Ku80s75",
"outputId": "26e0bf6f-bfff-4af0-af5b-df74ca2b9b8f"
},
"execution_count": 6,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"2023-05-09 08:01:19.844568: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.\n",
"To enable the following instructions: AVX2 AVX512F FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.\n",
"2023-05-09 08:01:20.743122: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT\n",
"WARNING:tensorflow:From /usr/local/lib/python3.10/dist-packages/tensorflow/python/compat/v2_compat.py:107: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version.\n",
"Instructions for updating:\n",
"non-resource variables are not supported in the long term\n",
"2023-05-09 08:01:22.404786: I tensorflow/compiler/xla/stream_executor/cuda/cuda_gpu_executor.cc:996] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355\n",
"2023-05-09 08:01:22.445670: I tensorflow/compiler/xla/stream_executor/cuda/cuda_gpu_executor.cc:996] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355\n",
"2023-05-09 08:01:22.446049: I tensorflow/compiler/xla/stream_executor/cuda/cuda_gpu_executor.cc:996] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355\n",
"2023-05-09 08:01:22.446985: I tensorflow/compiler/xla/stream_executor/cuda/cuda_gpu_executor.cc:996] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355\n",
"2023-05-09 08:01:22.447333: I tensorflow/compiler/xla/stream_executor/cuda/cuda_gpu_executor.cc:996] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355\n",
"2023-05-09 08:01:22.447599: I tensorflow/compiler/xla/stream_executor/cuda/cuda_gpu_executor.cc:996] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355\n",
"2023-05-09 08:01:23.713855: I tensorflow/compiler/xla/stream_executor/cuda/cuda_gpu_executor.cc:996] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355\n",
"2023-05-09 08:01:23.714245: I tensorflow/compiler/xla/stream_executor/cuda/cuda_gpu_executor.cc:996] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355\n",
"2023-05-09 08:01:23.714489: I tensorflow/compiler/xla/stream_executor/cuda/cuda_gpu_executor.cc:996] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero. See more at https://github.com/torvalds/linux/blob/v6.0/Documentation/ABI/testing/sysfs-bus-pci#L344-L355\n",
"2023-05-09 08:01:23.714654: W tensorflow/core/common_runtime/gpu/gpu_bfc_allocator.cc:47] Overriding orig_value setting because the TF_FORCE_GPU_ALLOW_GROWTH environment variable is set. Original config value was 0.\n",
"2023-05-09 08:01:23.714697: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1635] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 13678 MB memory: -> device: 0, name: Tesla T4, pci bus id: 0000:00:04.0, compute capability: 7.5\n",
">> Downloading net-lin_alex_v0.1.pb 100.1%\n",
"Successfully downloaded net-lin_alex_v0.1.pb 9947410 bytes.\n",
"2023-05-09 08:01:36.531668: W tensorflow/core/common_runtime/graph_constructor.cc:1533] Importing a graph with a lower producer version 27 into an existing graph with producer version 1395. Shape inference will have run different parts of the graph with different producer versions.\n",
"2023-05-09 08:01:36.750001: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:353] MLIR V1 optimization pass is not enabled\n",
"2023-05-09 08:01:40.364385: I tensorflow/compiler/xla/stream_executor/cuda/cuda_dnn.cc:424] Loaded cuDNN version 8700\n",
"Distances: (0.328)\n"
]
}
]
}
]
}
4 changes: 2 additions & 2 deletions lpips_tf.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import sys

import tensorflow as tf
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
from six.moves import urllib

_URL = 'http://rail.eecs.berkeley.edu/models/lpips'
Expand Down
3 changes: 2 additions & 1 deletion test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import cv2
import numpy as np
import tensorflow as tf
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

import lpips_tf

Expand Down