skyipeng commited on
Commit
15f800f
·
verified ·
1 Parent(s): 63f0ad1

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +14 -121
README.md CHANGED
@@ -239,132 +239,25 @@ pipeline_tag: image-text-to-text
239
 
240
  ## 4. Usage
241
 
242
- ```python
243
- import math
244
- import torch
245
- import torchvision.transforms as T
246
- from PIL import Image
247
- from torchvision.transforms.functional import InterpolationMode
248
- from transformers import AutoConfig, AutoModel, AutoTokenizer
249
 
250
- IMAGENET_MEAN = (0.485, 0.456, 0.406)
251
- IMAGENET_STD = (0.229, 0.224, 0.225)
252
-
253
- def build_transform(input_size):
254
- MEAN, STD = IMAGENET_MEAN, IMAGENET_STD
255
- transform = T.Compose([
256
- T.Lambda(lambda img: img.convert('RGB') if img.mode != 'RGB' else img),
257
- T.Resize((input_size, input_size), interpolation=InterpolationMode.BICUBIC),
258
- T.ToTensor(),
259
- T.Normalize(mean=MEAN, std=STD)
260
- ])
261
- return transform
262
-
263
- def find_closest_aspect_ratio(aspect_ratio, target_ratios, width, height, image_size):
264
- best_ratio_diff = float('inf')
265
- best_ratio = (1, 1)
266
- area = width * height
267
- for ratio in target_ratios:
268
- target_aspect_ratio = ratio[0] / ratio[1]
269
- ratio_diff = abs(aspect_ratio - target_aspect_ratio)
270
- if ratio_diff < best_ratio_diff:
271
- best_ratio_diff = ratio_diff
272
- best_ratio = ratio
273
- elif ratio_diff == best_ratio_diff:
274
- if area > 0.5 * image_size * image_size * ratio[0] * ratio[1]:
275
- best_ratio = ratio
276
- return best_ratio
277
-
278
- def dynamic_preprocess(image, min_num=1, max_num=12, image_size=448, use_thumbnail=False):
279
- orig_width, orig_height = image.size
280
- aspect_ratio = orig_width / orig_height
281
- target_ratios = set(
282
- (i, j) for n in range(min_num, max_num + 1) for i in range(1, n + 1) for j in range(1, n + 1) if
283
- i * j <= max_num and i * j >= min_num)
284
- target_ratios = sorted(target_ratios, key=lambda x: x[0] * x[1])
285
- target_aspect_ratio = find_closest_aspect_ratio(
286
- aspect_ratio, target_ratios, orig_width, orig_height, image_size)
287
- target_width = image_size * target_aspect_ratio[0]
288
- target_height = image_size * target_aspect_ratio[1]
289
- blocks = target_aspect_ratio[0] * target_aspect_ratio[1]
290
- resized_img = image.resize((target_width, target_height))
291
- processed_images = []
292
- for i in range(blocks):
293
- box = (
294
- (i % (target_width // image_size)) * image_size,
295
- (i // (target_width // image_size)) * image_size,
296
- ((i % (target_width // image_size)) + 1) * image_size,
297
- ((i // (target_width // image_size)) + 1) * image_size
298
- )
299
- split_img = resized_img.crop(box)
300
- processed_images.append(split_img)
301
- assert len(processed_images) == blocks
302
- if use_thumbnail and len(processed_images) != 1:
303
- thumbnail_img = image.resize((image_size, image_size))
304
- processed_images.append(thumbnail_img)
305
- return processed_images
306
-
307
- def load_image(image_file, input_size=448, max_num=12):
308
- image = Image.open(image_file).convert('RGB')
309
- transform = build_transform(input_size=input_size)
310
- images = dynamic_preprocess(image, image_size=input_size, use_thumbnail=True, max_num=max_num)
311
- pixel_values = [transform(image) for image in images]
312
- pixel_values = torch.stack(pixel_values)
313
- return pixel_values
314
 
315
- def split_model(model_path):
316
- device_map = {}
317
- world_size = torch.cuda.device_count()
318
- config = AutoConfig.from_pretrained(model_path, trust_remote_code=True)
319
- num_layers = config.llm_config.num_hidden_layers
320
- num_layers_per_gpu = math.ceil(num_layers / (world_size - 0.5))
321
- num_layers_per_gpu = [num_layers_per_gpu] * world_size
322
- num_layers_per_gpu[0] = math.ceil(num_layers_per_gpu[0] * 0.5)
323
- layer_cnt = 0
324
- for i, num_layer in enumerate(num_layers_per_gpu):
325
- for j in range(num_layer):
326
- device_map[f'language_model.model.layers.{layer_cnt}'] = i
327
- layer_cnt += 1
328
- device_map['vision_model'] = 0
329
- device_map['mlp1'] = 0
330
- device_map['language_model.model.tok_embeddings'] = 0
331
- device_map['language_model.model.embed_tokens'] = 0
332
- device_map['language_model.output'] = 0
333
- device_map['language_model.model.norm'] = 0
334
- device_map['language_model.model.rotary_emb'] = 0
335
- device_map['language_model.lm_head'] = 0
336
- device_map[f'language_model.model.layers.{num_layers - 1}'] = 0
337
- return device_map
338
 
339
- path = 'Skywork/Skywork-R1V-38B'
340
- device_map = split_model(path)
341
- model = AutoModel.from_pretrained(
342
- path,
343
- torch_dtype=torch.bfloat16,
344
- load_in_8bit=False,
345
- low_cpu_mem_usage=True,
346
- use_flash_attn=True,
347
- trust_remote_code=True,
348
- device_map=device_map).eval()
349
- tokenizer = AutoTokenizer.from_pretrained(path, trust_remote_code=True, use_fast=False)
350
- generation_config = dict(max_new_tokens=64000, do_sample=True, temperature=0.6, top_p=0.95, repetition_penalty=1.05)
351
 
352
- # single-image conversation
353
- pixel_values = load_image('./examples/image1.jpg', max_num=12).to(torch.bfloat16).cuda()
354
- question = '<image>\nSelect the correct option from this question.'
355
- response = model.chat(tokenizer, pixel_values, question, generation_config)
356
- print(f'User: {question}\nAssistant: {response}')
357
 
358
- # multi-image conversation
359
- pixel_values1 = load_image('./examples/image1.jpg', max_num=12).to(torch.bfloat16).cuda()
360
- pixel_values2 = load_image('./examples/image2.jpg', max_num=12).to(torch.bfloat16).cuda()
361
- pixel_values = torch.cat((pixel_values1, pixel_values2), dim=0)
362
- num_patches_list = [pixel_values1.size(0), pixel_values2.size(0)]
363
- question = '<image>\n<image>\nSelect the correct option from this question.'
364
- response, history = model.chat(tokenizer, pixel_values, question, generation_config,
365
- num_patches_list=num_patches_list,
366
- history=None, return_history=True)
367
- print(f'User: {question}\nAssistant: {response}')
368
  ```
369
 
370
  ---
 
239
 
240
  ## 4. Usage
241
 
242
+ ### step1. Clone the Repository
243
+ First, clone the repository to your local machine:
 
 
 
 
 
244
 
245
+ ```shell
246
+ git clone https://github.com/SkyworkAI/Skywork-R1V.git
247
+ cd skywork-r1v/inference
248
+ ```
249
+ ### step2. Set Up the Environment
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
250
 
251
+ ```shell
252
+ pip install -r requirements.txt
253
+ ```
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
254
 
255
+ #### step3. Run the Inference Script
 
 
 
 
 
 
 
 
 
 
 
256
 
257
+ Prepare your images and questions, and update them to inference_with_transformers.py
 
 
 
 
258
 
259
+ ```shell
260
+ python inference_with_transformers.py
 
 
 
 
 
 
 
 
261
  ```
262
 
263
  ---