# Tensorflow Pretrained

## Preprocessing Images

1. Put path of all images in a list
2. For all images, load image

```python
from tensorflow.python.keras.preprocessing.image import load_img
imgs = [load_img(img, target_size=(height, width), for img in imgs)]
```

1. Convert images into 3D tensors (an image is a 3D tensor as seen in the last lesson) using Keras `img_to_array()`. Multiple 3D tensors stack together to form another array.

```python
import numpy as np
from tensorflow.python.keras.preprocessing.image import img_to_array
img_array = np.array([img_to_array(img) for img in imgs])
```

1. Do some arithmetic on the new array using `preprocess_input()`. Squishes values to $$\[-1, 1]$$ since the pretrained model resnet also had these values.

```python
from tensorflow.python.keras.applications.resnet50 import preprocess_input
output = preprocess_input(img_array)
```

## Predicting using Pretrained Models

```python
from tensorflow.python.keras.applications import ResNet50

my_model = ResNet50(weights='../input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels.h5')
test_data = read_and_prep_images(img_paths)
preds = my_model.predict(test_data)
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lauradang.gitbook.io/notes/machine-learning/tensorflow/tensorflow-pretrained.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
