Repository: replicate/cog

Issue:
Dear friend,please tell me why I can't run it from cog example.
I  am a newbie.
I run the code from cog examples.
I can  run "cog run python",but I can't run following command.
input:
sudo cog predict -i @input.jpg
resluts:
![image](https://user-images.githubusercontent.com/83233614/158789282-e6641fd4-79da-40ac-b438-4b58bf1f14d7.png)



Discussion:
You're missing the parameter name from the parameter:

```
cog predict -i image=@input.jpg
```
> 



> Contributor

Thank you very much, but there's a new problem.
![image](https://user-images.githubusercontent.com/83233614/158800879-cf942b13-22fe-4552-87b4-d5c25c8930ca.png)

It looks like your model is returning a  value of type `float32` which is not one of Cog's currently [supported types](https://github.com/replicate/cog/blob/main/docs/python.md#inputkwargs). 

Is it possible for you to update your code to convert this value from a `float32` to a `float`?
I run the code from cog examples.
### **predict.py**

from typing import Any
from cog import BasePredictor, Input, Path
from tensorflow.keras.applications.resnet50 import ResNet50
from tensorflow.keras.preprocessing import image as keras_image
from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np


class ResNetPredictor(BasePredictor):
    def setup(self):
        """Load the model into memory to make running multiple predictions efficient"""
        self.model = ResNet50(weights='resnet50_weights_tf_dim_ordering_tf_kernels.h5')

    # Define the arguments and types the model takes as input
    def predict(self, image: Path = Input(description="Image to classify")) -> Any:
        """Run a single prediction on the model"""
        # Preprocess the image
        img = keras_image.load_img(image, target_size=(224, 224))
        x = keras_image.img_to_array(img)
        x = np.expand_dims(x, axis=0)
        x = preprocess_input(x)
        # Run the prediction
        preds = self.model.predict(x)
        # Return the top 3 predictions
        return decode_predictions(preds, top=3)[0]

cog.yaml
build:
  python_version: "3.8"
  python_packages:
    - pillow==8.3.1
    - tensorflow==2.5.0
predict: "predict.py:ResNetPredictor"

I want the result.
$ cog predict -i image=@input.jpg
...
[
  [
    "n02123159",
    "tiger_cat",
    0.4874822497367859
  ],
  [
    "n02123045",
    "tabby",
    0.23169134557247162
  ],
  [
    "n02124075",
    "Egyptian_cat",
    0.09728282690048218
  ]
]













**### _But I get the result._**
(base) vipuser@ubuntu1804:~/cog-quickstart$ sudo cog --debug predict -i image=@input.jpg
Building Docker image from environment in cog.yaml...
$ docker build --file - --build-arg BUILDKIT_INLINE_CACHE=1 --tag cog-cog-quickstart-base --progress auto .
[+] Building 4.3s (13/13) FINISHED
 => [internal] load build definition from Dockerfile                                                               0.3s
 => => transferring dockerfile: 591B                                                                               0.0s
 => [internal] load .dockerignore                                                                                  0.4s
 => => transferring context: 2B                                                                                    0.0s
 => resolve image config for docker.io/docker/dockerfile:1.2                                                       1.3s
 => CACHED docker-image://docker.io/docker/dockerfile:1.2@sha256:e2a8561e419ab1ba6b2fe6cbdf49fd92b95912df1cf7d313  0.0s
 => [internal] load metadata for docker.io/library/python:3.8                                                      1.6s
 => [internal] load build context                                                                                  0.1s
 => => transferring context: 25.59kB                                                                               0.0s
 => [stage-0 1/5] FROM docker.io/library/python:3.8@sha256:02991ad89f1fc4e37205832fe7b4886d7035bb0cd151d5dc4ebc25  0.0s
 => CACHED [stage-0 2/5] COPY .cog/tmp/build816126947/cog-0.0.1.dev-py3-none-any.whl /tmp/cog-0.0.1.dev-py3-none-  0.0s
 => CACHED [stage-0 3/5] RUN --mount=type=cache,target=/root/.cache/pip pip install /tmp/cog-0.0.1.dev-py3-none-a  0.0s
 => CACHED [stage-0 4/5] RUN --mount=type=cache,target=/root/.cache/pip pip install   pillow==8.3.1 tensorflow==2  0.0s
 => CACHED [stage-0 5/5] WORKDIR /src                                                                              0.0s
 => exporting to image                                                                                             0.3s
 => => exporting layers                                                                                            0.0s
 => => writing image sha256:7f757669aae2797a7898ca3260093660a465e2eaaf559f7fd11bf962006bbad0                       0.0s
 => => naming to docker.io/library/cog-cog-quickstart-base                                                         0.0s
 => exporting cache                                                                                                0.0s
 => => preparing build cache for export                                                                            0.0s

Starting Docker image cog-cog-quickstart-base and running setup()...
$ docker run --rm --shm-size 8G --detach --env COG_LOG_LEVEL=debug --publish 5081:5000 --mount type=bind,source=/home/vipuser/cog-quickstart,destination=/src cog-cog-quickstart-base
2022-03-22 08:53:58.110868: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcudart.so.11.0'; dlerror: libcudart.so.11.0: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: :/usr/lib/x86_64-linux-gnu:/usr/local/nvidia/lib64:/usr/local/nvidia/bin
2022-03-22 08:53:58.110930: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
INFO:     Started server process [1]
INFO:     Waiting for application startup.
2022-03-22 08:53:59.825196: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'libcuda.so.1'; dlerror: libcuda.so.1: cannot open shared object file: No such file or directory; LD_LIBRARY_PATH: :/usr/lib/x86_64-linux-gnu:/usr/local/nvidia/lib64:/usr/local/nvidia/bin
2022-03-22 08:53:59.825248: W tensorflow/stream_executor/cuda/cuda_driver.cc:326] failed call to cuInit: UNKNOWN ERROR (303)
2022-03-22 08:53:59.825311: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:163] no NVIDIA GPU device is present: /dev/nvidia0 does not exist
2022-03-22 08:53:59.825541: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 AVX512F FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://0.0.0.0:5000 (Press CTRL+C to quit)
INFO:     172.17.0.1:48550 - "GET / HTTP/1.1" 200 OK
Running prediction...
2022-03-22 08:54:01.839272: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:176] None of the MLIR Optimization Passes are enabled (registered 2)
2022-03-22 08:54:01.839892: I tensorflow/core/platform/profile_utils/cpu_utils.cc:114] CPU Frequency: 2100125000 Hz
Downloading data from https://storage.googleapis.com/download.tensorflow.org/data/imagenet_class_index.json
40960/35363 [==================================] - 0s 2us/step
INFO:     172.17.0.1:48554 - "POST /predictions HTTP/1.1" 500 Internal Server Error
ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/uvicorn/protocols/http/httptools_impl.py", line 372, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "/usr/local/lib/python3.8/site-packages/uvicorn/middleware/proxy_headers.py", line 75, in __call__
Stopping container...
    return await self.app(scope, receive, send)
  File "/usr/local/lib/python3.8/site-packages/fastapi/applications.py", line 261, in __call__
    await super().__call__(scope, receive, send)
  File "/usr/local/lib/python3.8/site-packages/starlette/applications.py", line 112, in __call__
    await self.middleware_stack(scope, receive, send)
  File "/usr/local/lib/python3.8/site-packages/starlette/middleware/errors.py", line 181, in __call__
    raise exc
  File "/usr/local/lib/python3.8/site-packages/starlette/middleware/errors.py", line 159, in __call__
    await self.app(scope, receive, _send)
  File "/usr/local/lib/python3.8/site-packages/starlette/exceptions.py", line 82, in __call__
    raise exc
  File "/usr/local/lib/python3.8/site-packages/starlette/exceptions.py", line 71, in __call__
    await self.app(scope, receive, sender)
  File "/usr/local/lib/python3.8/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in __call__
    raise e
  File "/usr/local/lib/python3.8/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in __call__
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.8/site-packages/starlette/routing.py", line 656, in __call__
    await route.handle(scope, receive, send)
  File "/usr/local/lib/python3.8/site-packages/starlette/routing.py", line 259, in handle
    await self.app(scope, receive, send)
  File "/usr/local/lib/python3.8/site-packages/starlette/routing.py", line 61, in app
    response = await func(request)
  File "/usr/local/lib/python3.8/site-packages/fastapi/routing.py", line 227, in app
    raw_response = await run_endpoint_function(
  File "/usr/local/lib/python3.8/site-packages/fastapi/routing.py", line 162, in run_endpoint_function
    return await run_in_threadpool(dependant.call, **values)
  File "/usr/local/lib/python3.8/site-packages/starlette/concurrency.py", line 39, in run_in_threadpool
    return await anyio.to_thread.run_sync(func, *args)
  File "/usr/local/lib/python3.8/site-packages/anyio/to_thread.py", line 28, in run_sync
    return await get_asynclib().run_sync_in_worker_thread(func, *args, cancellable=cancellable,
  File "/usr/local/lib/python3.8/site-packages/anyio/_backends/_asyncio.py", line 818, in run_sync_in_worker_thread
    return await future
  File "/usr/local/lib/python3.8/site-packages/anyio/_backends/_asyncio.py", line 754, in run
    result = context.run(func, *args)
  File "/usr/local/lib/python3.8/site-packages/cog/server/http.py", line 91, in predict
    return JSONResponse(content=encoded_response)
  File "/usr/local/lib/python3.8/site-packages/starlette/responses.py", line 49, in __init__
    self.body = self.render(content)
  File "/usr/local/lib/python3.8/site-packages/starlette/responses.py", line 174, in render
    return json.dumps(
  File "/usr/local/lib/python3.8/json/__init__.py", line 234, in dumps
    return cls(
  File "/usr/local/lib/python3.8/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/local/lib/python3.8/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
  File "/usr/local/lib/python3.8/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type float32 is not JSON serializable
INFO:     Shutting down
INFO:     Waiting for application shutdown.
INFO:     Application shutdown complete.
INFO:     Finished server process [1]
ⅹ /predictions call returned status 500

Resolving patch:
diff --git a/python/cog/json.py b/python/cog/json.py
--- a/python/cog/json.py
+++ b/python/cog/json.py
@@ -1,5 +1,6 @@
 from enum import Enum
 import io
+from types import GeneratorType
 from typing import Any
 
 from pydantic import BaseModel
@@ -26,7 +27,7 @@ def encode_json(obj: Any, upload_file) -> Any:
         return encode_json(obj.dict(exclude_unset=True), upload_file)
     if isinstance(obj, dict):
         return {key: encode_json(value, upload_file) for key, value in obj.items()}
-    if isinstance(obj, list):
+    if isinstance(obj, (list, set, frozenset, GeneratorType, tuple)):
         return [encode_json(value, upload_file) for value in obj]
     if isinstance(obj, Enum):
         return obj.value


Test patch:
diff --git a/python/tests/test_json.py b/python/tests/test_json.py
--- a/python/tests/test_json.py
+++ b/python/tests/test_json.py
@@ -9,6 +9,11 @@
 from pydantic import BaseModel
 
 
+def test_encode_json_recursively_encodes_tuples():
+    result = encode_json((np.float32(0.1), np.float32(0.2)), None)
+    assert type(result[0]) == float
+
+
 def test_encode_json_encodes_pydantic_models():
     class Model(BaseModel):
         text: str